scanf() driving me crazy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scorpio2002
    New Member
    • Nov 2006
    • 2

    scanf() driving me crazy

    Hi there. I'm new to the forum ;-)

    I'm having really a bad time with the scanf(9 function. Take a look at this:


    Code:
    #include <stdio.h>
    
    int main() {
    	char up_key,
    		down_key,
    		right_key,
    		left_key;
    			
    	int speed;	
    	printf("**** Configurazione delle opzioni ****\n");
    	printf("Immetti il valore up_key: ");
    	scanf("%c", &up_key);
    	printf("Immetti il valore down_key: ");
    	scanf("%c", &down_key);
    	printf("Immetti il valore right_key: ");
    	scanf("%c", &right_key);
    	printf("Immetti il valore left_key: ");
    	scanf("%c", &left_key);
    	printf("Immetti il timeout del gioco (più è alto, più il gioco è lento): ");
    	scanf("%d", &speed);
    	
    	}
    That is supposed to take some input from the user. The proble is that when I execute it, here's what I get:

    Code:
    **** Configurazione delle opzioni ****
    Immetti il valore up_key: e
    Immetti il valore down_key: Immetti il valore right_key: e
    Immetti il valore left_key: Immetti il timeout del gioco (più è alto, più il gioco è lento): 5
    Basically, it skeeps the 2nd and the 4th scanf(). I can't see any reason for that. I don't think it's a bug in gcc (the compiler i'm using)...

    Thanks in advance. That's really gonna drive me crazy :(
  • sundarr
    New Member
    • Aug 2006
    • 9

    #2
    It seems that repeated scanfs are not flushing out the read character from the terminal. so what u do is to include a fflush(stdin) before your every scanf call to avoid erronous behaviours.
    #include <stdio.h>

    int main() {
    char up_key,
    down_key,
    right_key,
    left_key;

    int speed;
    printf("**** Configurazione delle opzioni ****\n");
    printf("Immetti il valore up_key: ");
    scanf("%c", &up_key);
    fflush(stdin);
    printf("Immetti il valore down_key: ");
    scanf("%c", &down_key);
    fflush(stdin);
    printf("Immetti il valore right_key: ");
    scanf("%c", &right_key);
    fflush(stdin);
    printf("Immetti il valore left_key: ");
    scanf("%c", &left_key);
    fflush(stdin);
    printf("Immetti il timeout del gioco (più è alto, più il gioco è lento): ");
    scanf("%d", &speed);

    }

    This should work well for you.

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      When using the %c conversion specification with scanf() it returns all characters including spaces and <RETURN> (or <ENTER>) characters

      When you type in your input you hit the character followed by the <Enter> key.
      The first scanf() reads the character and the second scanf() (which should read your next character input) reads the <RETURN> character. What you can do is to tell scanf() to read up the the next non-white space character (skipping white-space). You do this by putting a space in the conversion specification, e.g. " %c"
      Code:
      #include <stdio.h>
      
      int main() {
      	char up_key,
      		down_key,
      		right_key,
      		left_key;
      			
      	int speed;	
      	printf("**** Configurazione delle opzioni ****\n");
      	printf("Immetti il valore up_key: ");
      	scanf(" %c", &up_key);
      	printf("Immetti il valore down_key: ");
      	scanf(" %c", &down_key);
      	printf("Immetti il valore right_key: ");
      	scanf(" %c", &right_key);
      	printf("Immetti il valore left_key: ");
      	scanf(" %c", &left_key);
      	printf("Immetti il timeout del gioco (più è alto, più il gioco è lento): ");
      	scanf("%d", &speed);
      	
      	}

      Comment

      Working...