473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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:


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
2 1912
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
horace1
1,510 Expert 1GB
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

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

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
0
by: Shapper | last post by:
Hello, I have this code in Global.asax: Sub Session_Start(Sender As Object, E As EventArgs) Dim cookie As HttpCookie = Request.Cookies("MyCookie") If Not cookie Is Nothing Then...
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
185
by: Martin Jørgensen | last post by:
Hi, Consider: ------------ char stringinput ..bla. bla. bla. do {
1
by: Miguel Dias Moura | last post by:
Hello, I have been trying, for days, to retrieve a control's ClientId in a javascript function. I am using a master page and this is why I need to retrieve the Control's ClientId. The control...
5
by: Pupeno | last post by:
Hello, I am experiencing a weird behavior that is driving me crazy. I have module called Sensors containing, among other things: class Manager: def getStatus(self): print "getStatus(self=%s)"...
3
by: rashpal.sidhu | last post by:
Please help, this problem is driving me crazy !! I am using metaphone to create phonetic keys. When i run the module stand-a-lone it works fine. I'm trying to create a runner for informix...
5
by: mark4asp | last post by:
Every time the function below is called I get the alert. So I put a deliberate error in there and I check the value of (reportType=='MANDATE') in Firebug, which is found to be true. But still the...
2
by: kheitmann | last post by:
OK, so I have a blog. I downloaded the "theme" from somewhere and have edited a few areas to suit my needs. There are different font themes within the page theme. Long story short, my "Text Posts"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.