PDA

View Full Version : This program was a triumph.



dragos240
April 7th, 2010, 01:26 AM
I'm just making a note here, about my program, which I feel is a HUGE SUCCESS. I'm very GLaD, I made it. It's hard to overstate my satisfaction. My first C program, 122 lines. 5 hours of work, including: Typing, Fixing errors, Syncing, Spell Checking, And finalizing. Not really my "First", but more my "First One I made almost (http://ubuntuforums.org/showthread.php?t=1448207)entirely myself."

Note the almost. Just extract the contents of the below file to your home folder. Open a terminal, cd into "still\ alive" and run the program by:
./stillalive
Link:
http://uppit.com/v/YS8MAWWY

I know it's not much, but it's my first step on the road of being a programmer.

Oh, and make sure you're using a Linux operating system (I think FreeBSD works too).

And to use one of the commands used, you need ALSA. Most people have this.

Yvan300
April 7th, 2010, 01:40 AM
What does it do?

dragos240
April 7th, 2010, 01:42 AM
What does it do?

Well.... Apparently you haven't played portal. I made the title and half of the post quotes from the song "still alive". Just download and see. If you don't trust it, view the source code, which is in the same folder.

nmccrina
April 7th, 2010, 01:53 AM
Cool, I love that song, even though I've never played Portal or Half-Life!

dragos240
April 7th, 2010, 01:56 AM
Cool, I love that song, even though I've never played Portal or Half-Life!

They're all awesome!

sisco311
April 7th, 2010, 01:57 AM
pleaseindentationplease

dragos240
April 7th, 2010, 01:58 AM
pleaseindentationplease

Sorry about that. :redface:

I'm still pretty new to programming.

nmccrina
April 7th, 2010, 02:05 AM
pleaseindentationplease

Overrated. I prefer this style:



#include <unistd.h>
#include <stdio.h>
int main(void){if(fork( ) != 0){system("aplay ./stillalive.raw&&exit");return 0;}{sleep(1);printf("This was a triumph.\n");sleep(3);printf("I'm making a note here.\n");sleep(2);printf("HUGE SUCCESS!\n");sleep(3);printf("It's hard to overstate my Satisfaction!\n");sleep(7);printf("Aperture Science!\n");sleep(4);printf("We do what we must, because we can.\n");sleep(5);printf("For the good of all of us,\n");sleep(4);printf("Except the ones who are dead...\n");sleep(2);printf("\n");printf("But there's no sense crying over every mistake.\n");sleep(4.5);printf("You just keep on trying 'till you run out of cake.\n");sleep(4.5);printf("And the science gets done,\n");sleep(2);printf("And you make a neat gun,\n");sleep(2.5);printf("For the people who are still alive!\n");sleep(8);printf("\n");sleep(1.5);printf("I'm not even angry.\n");sleep(4.5);printf("I'm being so sincere right now!\n");sleep(5.5);printf("Even though you broke my heart and killed me...\n");sleep(7);printf("And tore me to pieces.\n");sleep(4.5);printf("And threw every piece into a fire.\n");sleep(5);printf("As they burned, it hurt,\n");sleep(3);printf("Because I was so happy for you!\n");sleep(3);printf("\n");printf("Now these points of data make a beautiful line.\n");sleep(4);printf("And we're out of beta, we're releasing on time.\n");sleep(4);printf("So I'm GLaD I got burned,\n");sleep(2.5);printf("Think of all the things we learned.\n");sleep(2.5);printf("For the people who are still alive!\n");sleep(8);printf("\n");sleep(1);printf("Go ahead and leave me...\n");sleep(4);printf("I think I prefer to stay inside.\n");sleep(5.5);printf("Maybe you'll find someone else to help you...\n");sleep(7.);printf("Maybe Black Mesa!\n");sleep(4);printf("That was a joke,\n");sleep(2);printf("HA HA!\n");sleep(1.5);printf("Fat chance.\n");sleep(2);printf("Anyway, this cake is great,\n");sleep(4);printf("It's so delicious and moist!\n");sleep(2.5);printf("\n");printf("Look at me still talking when there's science to do!\n");sleep(4.5);printf("When I look up there, it makes me GLaD I'm not you!\n");sleep(4.5);printf("I've experiments to run!\n");sleep(2.5);printf("There is research to be done.\n");sleep(2.5);printf("On the people who are still alive.\n");sleep(3);printf("\n");printf("And believe me, I am:\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf("I'm doing science, and I'm:\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf("I feel FANTASTIC, and I'm:\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf("While you're dying, I'll be:\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf("And when you're dead, I will be:\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf(" SSSSS tt iii lll lll AAA lll iii !!!\nSS tt lll lll AAAAA lll vv vv eee !!!\n SSSSS tttt iii lll lll AA AA lll iii vv vv ee e !!!\n SS tt iii lll lll AAAAAAA lll iii vvv eeeee\n SSSSS tttt iii lll lll AA AA lll iii v eeeee !!!\n");return 0;}}

v1ad
April 7th, 2010, 02:13 AM
did you use a development environment or just notebook? can't find any good programs that i can use.

sisco311
April 7th, 2010, 02:19 AM
Overrated. I prefer this style:



#include <unistd.h>
#include <stdio.h>
int main(void){if(fork( ) != 0){system("aplay ./stillalive.raw&&exit");return 0;}{sleep(1);printf("This was a triumph.\n");sleep(3);printf("I'm making a note here.\n");sleep(2);printf("HUGE SUCCESS!\n");sleep(3);printf("It's hard to overstate my Satisfaction!\n");sleep(7);printf("Aperture Science!\n");sleep(4);printf("We do what we must, because we can.\n");sleep(5);printf("For the good of all of us,\n");sleep(4);printf("Except the ones who are dead...\n");sleep(2);printf("\n");printf("But there's no sense crying over every mistake.\n");sleep(4.5);printf("You just keep on trying 'till you run out of cake.\n");sleep(4.5);printf("And the science gets done,\n");sleep(2);printf("And you make a neat gun,\n");sleep(2.5);printf("For the people who are still alive!\n");sleep(8);printf("\n");sleep(1.5);printf("I'm not even angry.\n");sleep(4.5);printf("I'm being so sincere right now!\n");sleep(5.5);printf("Even though you broke my heart and killed me...\n");sleep(7);printf("And tore me to pieces.\n");sleep(4.5);printf("And threw every piece into a fire.\n");sleep(5);printf("As they burned, it hurt,\n");sleep(3);printf("Because I was so happy for you!\n");sleep(3);printf("\n");printf("Now these points of data make a beautiful line.\n");sleep(4);printf("And we're out of beta, we're releasing on time.\n");sleep(4);printf("So I'm GLaD I got burned,\n");sleep(2.5);printf("Think of all the things we learned.\n");sleep(2.5);printf("For the people who are still alive!\n");sleep(8);printf("\n");sleep(1);printf("Go ahead and leave me...\n");sleep(4);printf("I think I prefer to stay inside.\n");sleep(5.5);printf("Maybe you'll find someone else to help you...\n");sleep(7.);printf("Maybe Black Mesa!\n");sleep(4);printf("That was a joke,\n");sleep(2);printf("HA HA!\n");sleep(1.5);printf("Fat chance.\n");sleep(2);printf("Anyway, this cake is great,\n");sleep(4);printf("It's so delicious and moist!\n");sleep(2.5);printf("\n");printf("Look at me still talking when there's science to do!\n");sleep(4.5);printf("When I look up there, it makes me GLaD I'm not you!\n");sleep(4.5);printf("I've experiments to run!\n");sleep(2.5);printf("There is research to be done.\n");sleep(2.5);printf("On the people who are still alive.\n");sleep(3);printf("\n");printf("And believe me, I am:\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf("I'm doing science, and I'm:\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf("I feel FANTASTIC, and I'm:\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf("While you're dying, I'll be:\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf("And when you're dead, I will be:\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf("Still Alive!\n");sleep(2.5);printf(" SSSSS tt iii lll lll AAA lll iii !!!\nSS tt lll lll AAAAA lll vv vv eee !!!\n SSSSS tttt iii lll lll AA AA lll iii vv vv ee e !!!\n SS tt iii lll lll AAAAAAA lll iii vvv eeeee\n SSSSS tttt iii lll lll AA AA lll iii v eeeee !!!\n");return 0;}}


I guess you like episode 18 from James Joyce's Ulysses & brainf?ck too:


++++++++++[>+++++++>+++>+++++++++++<<<-]>+++.>++.>--.+++.+++++++.-----------------.<.<++++++++++++.>>---.+++++++++++++++++++.-------.++++++.+.

:)

chris200x9
April 7th, 2010, 02:20 AM
you should loop it, :) but cool little program.

Yes
April 7th, 2010, 02:24 AM
I recommend using a program like Geany, for auto tabbing and such.

You also seem to have some brackets around most of the program which are unnecessary.

the yawner
April 7th, 2010, 02:32 AM
Feature request: Display lyrics in the same manner as the original (i.e. typing effect). :D

-grubby
April 7th, 2010, 02:40 AM
Punny original post.

dragos240
April 7th, 2010, 02:43 AM
Feature request: Display lyrics in the same manner as the original (i.e. typing effect). :D

I actually tried doing this:

printf("T");
sleep(0.3);
printf("h");
sleep(0.3);
printf("i");
sleep(0.3);
printf("s");

For a typed effect of "This".

But it didn't work. :(

nmccrina
April 7th, 2010, 02:49 AM
I guess you like episode 18 from James Joyce's Ulysses & brainf?ck too:


++++++++++[>+++++++>+++>+++++++++++<<<-]>+++.>++.>--.+++.+++++++.-----------------.<.<++++++++++++.>>---.+++++++++++++++++++.-------.++++++.+.

:)

Haha, a couple of days ago I decided to write a brainf?ck compiler (an interpreter would be just too easy :P ). So now I'm learning assembly. :shock:

I'll admit that I'm a bit of a programming masochist, but it's fun! Still, I don't think I would actually code like that...unless it was part of some obfuscated programming contest.

red_Marvin
April 7th, 2010, 03:05 AM
I actually tried doing this:

printf("T");
sleep(0.3);
printf("h");
sleep(0.3);
printf("i");
sleep(0.3);
printf("s");

For a typed effect of "This".

But it didn't work. :(
I am pretty sure that it is because the terminal is line buffered, ie it waits for a newline before it changes stuff.

dragos240
April 7th, 2010, 03:08 AM
I am pretty sure that it is because the terminal is line buffered, ie it waits for a newline before it changes stuff.
Any idea on how I can do this?

red_Marvin
April 7th, 2010, 03:18 AM
Maybe this will help:http://www.cplusplus.com/reference/clibrary/cstdio/setvbuf/ or general googling on stdio and unbuffered...
If one wants to do more advanced stuff, colours, positions it is often suggested to use ncurses.

crunchy3neo
April 7th, 2010, 04:18 AM
Awesome program :)

phrostbyte
April 7th, 2010, 05:17 AM
I actually tried doing this:

printf("T");
sleep(0.3);
printf("h");
sleep(0.3);
printf("i");
sleep(0.3);
printf("s");

For a typed effect of "This".

But it didn't work. :(

That's because the function "sleep" takes a parameter of type "unsigned int". This type represents positive "integers", a kind of number that can not be decimal.

What is happening is the decimal portion is being clipped off, so you are calling "sleep(0)". Also, all your calls to sleep(4.5) are actually the same as sleep(4).

There is actually a system call with better precision then sleep called "nanosleep", which lets you define sleep intervals in nanoseconds, but it is complicated to use.

phrostbyte
April 7th, 2010, 05:22 AM
If you are going to do the per character thing, you should probably define it as a function. Learn more about C functions and also C strings, and for and while loops.

J V
April 7th, 2010, 12:58 PM
Yes, not sure about C but python would be something along the lines of:


def slowprint(stringname):
for character in stringname:
time.sleep(0.1)
echo -n characterFairly simple that way, just pass a string :)

Edit: Was able to do in bash something along the lines of:

for character in stringname
do
$command = $command + ' && sleep 0.1 && echo -n "$character"'
done

red_Marvin
April 7th, 2010, 01:09 PM
def slowprint(stringname):
for character in stringname:
time.sleep(0.1)
echo -n character
echo is shellscript (and in shellscript it would append a newline edit: nope it won't, but without the -n switch it will), in python one can use print character - but that also appends a newline or print character, but that appends a space.
The only way I've found is using sys.stdout.write(character), which requires that one imports sys.

dragos240
April 7th, 2010, 01:51 PM
Also another idea is to use screen to attach 2 seperate terminals to to seperate x terms. The lyrics will run in one, and the ascii art will run in another.

Tibuda
April 7th, 2010, 02:04 PM
echo is shellscript (and in shellscript it would append a newline), in python one can use print character - but that also appends a newline or print character, but that appends a space.
The only way I've found is using sys.stdout.write(character), which requires that one imports sys.

"echo -n" will not append a new line in bash

whiskeylover
April 7th, 2010, 02:55 PM
Any idea on how I can do this?

flush() the buffer after each print();

phrostbyte
April 7th, 2010, 05:06 PM
Maybe this will help:http://www.cplusplus.com/reference/clibrary/cstdio/setvbuf/ or general googling on stdio and unbuffered...
If one wants to do more advanced stuff, colours, positions it is often suggested to use ncurses.

Oh yes definitely. I _think_ this will work (I didn't actually test it).
You need the ncurses development headers which is libncurses5-dev I think, and the -lncurses flag in gcc.



#include <unistd.h>
#include <ncurses.h>

/* ... */


void display_marquee(char* str, int x, int y, useconds_t msecs)
{
for (int i=0; i!='\0'; i++) {
mvaddch(y, x + i, str[i]);
usleep(msecs);
}

}


Of course you can add things like colors, and make it all fancy if you'd like.

red_Marvin
April 7th, 2010, 11:45 PM
"echo -n" will not append a new line in bash

My bad, post edit pending.