reading integer input from user in C

Member
Posts: 61
Joined: 2007.01
Post: #1
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.

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
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
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.
Quote this message in a reply
Member
Posts: 61
Joined: 2007.01
Post: #3
Thanks OneSadCookie, I'll change my code to use your suggestions.
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #4
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.

It's not magic, it's Ruby.
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
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!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Accelerate and working with Integer Arrays SIMD Bersaelor 6 6,038 Jul 6, 2010 07:56 AM
Last Post: Bersaelor
  Weird problem passing integer variables.. quarus 6 4,912 Mar 15, 2009 12:47 PM
Last Post: quarus
  Getting a function to recognize an integer FlamingHairball 7 4,848 Jan 20, 2008 06:35 AM
Last Post: FlamingHairball