PDA

View Full Version : Learning C




larswik
Dec 4, 2010, 01:53 PM
Had a quick question. I am writing a small bit of code to help do some math for a game we play. I first have it ask for a number and then ask for a letter. When I tested the first part of this code it hangs in the console and won't move to the next question.

#include <stdio.h>

int main (int argc, const char * argv[]) {
int level;
char crit;

printf("Enter the level of the Character:");
scanf( "%d\n", &level );

printf("Enter the Crit letter:");
scanf( "%c", &crit );

return 0;
}

I am t a loss to understand why it won't print the second printf line. It seems so simple? The code is not finished yet but this little part should work even though the code is not completed yet I wold think.

Thanks

-Lars



mac2x
Dec 4, 2010, 03:24 PM
1. Get rid of the newline in the first call to scanf().

2. Understand the behavior of %c in scanf().

chown33
Dec 4, 2010, 04:30 PM
1. Get rid of the newline in the first call to scanf().

2. Understand the behavior of %c in scanf().

0. Understand the role of whitespace (which includes newlines) in scanf patterns. Recommended minimum: Read The Fine Man Page (RTFMP).

larswik
Dec 4, 2010, 08:02 PM
I'll read up on 'White Spaces'.

Thanks

-Lars