Learning C

Discussion in 'Mac Programming' started by larswik, Dec 4, 2010.

  1. larswik macrumors 68000

    Sep 8, 2006
    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.


  2. mac2x macrumors 65816

    Sep 19, 2009
    1. Get rid of the newline in the first call to scanf().

    2. Understand the behavior of %c in scanf().
  3. chown33 macrumors 604

    Aug 9, 2009
    descending into the Maelström
    0. Understand the role of whitespace (which includes newlines) in scanf patterns. Recommended minimum: Read The Fine Man Page (RTFMP).
  4. larswik thread starter macrumors 68000

    Sep 8, 2006

Share This Page