reading integer input from user in C
Hi,
I'm trying to read in some input from the user in C.
They enter a char to select an action and then input an integer value.
e.g.
I've noticed that scanf() seems to eat up an extra char or something like that.
It thinks that there has been a char already entered the next time it comes around to the "move, shoot or quit".
Are there any alternatives to scanf() ? Or am I doing it wrong?
Hopefully I've explained the problem correctly, I'm trying to implement a version of the Hunt The Wumpus game for a C programming module.
Thanks,
Anthony
I'm trying to read in some input from the user in C.
They enter a char to select an action and then input an integer value.
e.g.
Code:
do
{
printf("What would you like to do? (m) move, (s) shoot or (q) quit\n");
action = getchar();
/* trap Enter key press */
getchar();
} while ( action != 'm' && action != 's' && action != 'q' );
switch(action)
{
case 'm':
printf("\nMove to which room?");
scanf("%d", &moveTo);
...
...
...
}I've noticed that scanf() seems to eat up an extra char or something like that.
It thinks that there has been a char already entered the next time it comes around to the "move, shoot or quit".
Quote:e.g.
What would you like to do? (m) move, (s) shoot or (q) quit
m
Move to which room?
2
What would you like to do? (m) move, (s) shoot or (q) quit
m
(When I checked the contents it says action contains a ' ' (blank) and not an 'm'.)
What would you like to do? (m) move, (s) shoot or (q) quit
(I press Enter)
What would you like to do? (m) move, (s) shoot or (q) quit
(seems to clear the problem)
m
Move to which room?
3
Are there any alternatives to scanf() ? Or am I doing it wrong?
Hopefully I've explained the problem correctly, I'm trying to implement a version of the Hunt The Wumpus game for a C programming module.
Thanks,
Anthony
The best way to handle this kind of interaction in C seems to be to *always* use fgets to read lines from standard input, then use sscanf or atoi to parse the results. Mixing getchar, scanf, fread, etc. causes nothing but problems.
use gets and atoi; fscanf is rather an overkill for just the reading of an integer, and (I think) fscanf calls atoi anyway.
so gets() the line, then atoi() it, and give an error if atoi fails.
so gets() the line, then atoi() it, and give an error if atoi fails.
It's not magic, it's Ruby.
Nayr Wrote:use gets and atoi; fscanf is rather an overkill for just the reading of an integer, and (I think) fscanf calls atoi anyway.
so gets() the line, then atoi() it, and give an error if atoi fails.
Do not use gets.
atoi is deprecated, use strtol or something else instead.
Sir, e^iπ + 1 = 0, hence God exists; reply!
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Accelerate and working with Integer Arrays SIMD | Bersaelor | 6 | 5,026 |
Jul 6, 2010 07:56 AM Last Post: Bersaelor |
|
| Weird problem passing integer variables.. | quarus | 6 | 4,109 |
Mar 15, 2009 12:47 PM Last Post: quarus |
|
| Getting a function to recognize an integer | FlamingHairball | 7 | 4,298 |
Jan 20, 2008 06:35 AM Last Post: FlamingHairball |
|

