By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

scanf() driving me crazy

P: 2
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:


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.     char up_key,
  5.         down_key,
  6.         right_key,
  7.         left_key;
  8.  
  9.     int speed;    
  10.     printf("**** Configurazione delle opzioni ****\n");
  11.     printf("Immetti il valore up_key: ");
  12.     scanf("%c", &up_key);
  13.     printf("Immetti il valore down_key: ");
  14.     scanf("%c", &down_key);
  15.     printf("Immetti il valore right_key: ");
  16.     scanf("%c", &right_key);
  17.     printf("Immetti il valore left_key: ");
  18.     scanf("%c", &left_key);
  19.     printf("Immetti il timeout del gioco (pi  alto, pi il gioco  lento): ");
  20.     scanf("%d", &speed);
  21.  
  22.     }
  23.  
That is supposed to take some input from the user. The proble is that when I execute it, here's what I get:

Expand|Select|Wrap|Line Numbers
  1. **** Configurazione delle opzioni ****
  2. Immetti il valore up_key: e
  3. Immetti il valore down_key: Immetti il valore right_key: e
  4. 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 :(
Nov 3 '06 #1
Share this Question
Share on Google+
2 Replies


P: 9
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.
Nov 4 '06 #2

Expert 100+
P: 1,510
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"
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.     char up_key,
  5.         down_key,
  6.         right_key,
  7.         left_key;
  8.  
  9.     int speed;    
  10.     printf("**** Configurazione delle opzioni ****\n");
  11.     printf("Immetti il valore up_key: ");
  12.     scanf(" %c", &up_key);
  13.     printf("Immetti il valore down_key: ");
  14.     scanf(" %c", &down_key);
  15.     printf("Immetti il valore right_key: ");
  16.     scanf(" %c", &right_key);
  17.     printf("Immetti il valore left_key: ");
  18.     scanf(" %c", &left_key);
  19.     printf("Immetti il timeout del gioco (pi  alto, pi il gioco  lento): ");
  20.     scanf("%d", &speed);
  21.  
  22.     }
  23.  
Nov 4 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.