Page 1 of 1

My first C program [Help]

Posted: Wed Dec 30, 2009 9:01 pm
by Dex
First off, let me say that I am only 12 years old.
With that being said, my brain power is no match to yours ;)

I am learning C as my first programming language.
The book I am reading is "Absolute Beginners Guide to C".
I am working on my first real, interactive program.
Heres the source:

Code: Select all

/* Written by Dex*/
/* -Do you like my name?- Written in C++ */ 
#include <stdio.h>
main()
{
char name[4];  /* The variable that stores the yes/no choice */

printf("Hello, i'm Dex, do you like my name? [Say yes or no] \n"); /* Asking for you
to enter 'yes' or 'no' */

scanf("%c", name[4]); /* Character input for the 'name' variable */


if(name == "yes") /* If the user enters the word 'yes' */
{
        
printf("/n Really? Thanks, I like my name, too"); /* Happy message */

}


else

printf("/n What? You don't like my name? -Sadface-"); /* Sad message */
}

But when I type my 'yes or no' response, the program crashes!
Can anybody please explain this?



Edit: Please ignore the horrible whitespace used in my program, i think i am messing up spacing in some parts.
Thanks, Dex.

Re: My first C program [Help]

Posted: Wed Dec 30, 2009 9:15 pm
by hurstshifter
Welcome Dex! Glad to see you are interested in learning C. A few things...

1. You should get into the habit of setting the return value and formal parameters of your functions, even main. So instead of

Code: Select all

main()
you should have

Code: Select all

int main(void)
Your compiler probably compiled the original without any issues but when you start creating your own custom functions they will all have these features.

2. You are using the scanf() function to accept a char from the end user (%c), but are actually looking for the full "yes" or "no" response. In this situation you should use %s to scan in an entire string of characters. You'll learn more about strings later.

3. main() should be returning a value (typically 0 as long as your program ended normally). Finish main with

Code: Select all

return 0;
4. Your program probably is not crashing but just running extremely quickly and closing. Most IDEs will have an option to run the application with debugging which will prevent this from occurring. Otherwise, you can place this line at the end of your code to have it pause

Code: Select all

system("pause");

Re: My first C program [Help]

Posted: Wed Dec 30, 2009 9:17 pm
by Dex
Thank you so much for the help :)
This info was not included in my book (probably because it is a bit dated).

Re: My first C program [Help]

Posted: Wed Dec 30, 2009 9:27 pm
by Trask
hurstshifter wrote: 4. Your program probably is not crashing but just running extremely quickly and closing. Most IDEs will have an option to run the application with debugging which will prevent this from occurring. Otherwise, you can place this line at the end of your code to have it pause

Code: Select all

system("pause");
I really hope I don't come off as an ass, but just as a suggestion since you're making them ...about 2 years ago I broke myself out of using

Code: Select all

system("pause"); 
in favor of

Code: Select all

getchar();
simply from a mindset that assumes that your OS is not Windows, so better portability. Is it wrong to use? No, but again it was a habit that I found myself going "Huh, that's not going to work here, what do I use now?"

Re: My first C program [Help]

Posted: Thu Dec 31, 2009 10:35 am
by Dex
Thank you, Trask, I will keep that in mind. :bow:

Re: My first C program [Help]

Posted: Thu Dec 31, 2009 11:19 am
by hurstshifter
Trask wrote: I really hope I don't come off as an ass, but just as a suggestion since you're making them ...about 2 years ago I broke myself out of using

Code: Select all

system("pause"); 
in favor of

Code: Select all

getchar()
"
Excellent point

Re: My first C program [Help]

Posted: Sat Jan 02, 2010 12:04 pm
by Master Jake
Correct me if I'm wrong; but, aside from the errors mentioned above:

Along with %s as mentioned above, use "name" without an index to specify the full string

Code: Select all

scanf("%s", name);
C doesn't have string comparison available because it is not an object. This being said, "name == something" is invalid. You need the string comparison
function strcmp

Code: Select all

if(strcmp(name, "yes") == 0)
Side note: scanf is a bad function for reading in characters from the console as it can lead to buffer overflows and had little protection. As soon as you can, stray away from that.