Learning C

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

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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.

    Code:
    #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
     
  2. macrumors 65816

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

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

    Joined:
    Aug 9, 2009
    #3
    0. Understand the role of whitespace (which includes newlines) in scanf patterns. Recommended minimum: Read The Fine Man Page (RTFMP).
     
  4. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    I'll read up on 'White Spaces'.

    Thanks

    -Lars
     

Share This Page