Character input leading to default of switch statement always

From: Phoe6 (orsenthil_at_gmail.com)
Date: 02/27/05


Date: 27 Feb 2005 10:50:41 -0800

The Program Fragment is this:

int choice; /* Users Input Command */
.
.
.
printf("Enter a command: ");
fflush(stdin);
choice = getc(stdin);
printf("\n");
.
.
.
while(choice!='Q')
        {
                switch(choice)
                {
                        /* Movement options */
                        case 'n':
                                        dx = 0;
                                        dy = -1;
                                        action = 1;
                                        break;
                        case 's':
.
.
.
.
.
                        default:
                                        printf("You cannot do that!\n");
                                        action = 0;
                                        break;

                }

.
.
.
printf("Enter a Command :");
        fflush(stdin);
        choice = getc(stdin);
        printf("\n");

} /* <- End Brace of while loop */

The Problem I am observing is the Default Section of the Switch command
is being entering all the times (except for the first time).

I am guessing that the choice variable is getting someother value which
is causing it to enter the default; but using
getchar,getc(stdin),fgetc(stdin) and flushing the stdin is not solving
the problem.

---
The Complete Source (with the bug) is here:
http://geocities.com/uthcode2/wumpus.c
'Hunt the Wumpus' Game.
You can see the problem, when compiled and executed(on Linux,gcc)
Regards,
Senthil


Relevant Pages

  • Activation group problem
    ... I made an utility with some srvpgm a command and a command processing program that bind to a srvpgm. ... Recreating the srvpgm with a named activation group, as suggested by CEE3101 message, it runs fine even the first time. ... This way, any global varaible used by the service program is not the same as the program sees, so the content is lost. ...
    (comp.sys.ibm.as400.misc)
  • Re: Example of English Usage; Never mind the politics. Part II.
    ... I never use it in conversation, and this may be the first time I've used ... in many programming languages the command to output a line of text, ... regardless of its destination, is "print". ... clumsier method would have been to require the "ls" command to accept user ...
    (alt.usage.english)
  • Re: list files but not directory
    ... remember so you don't have to remember the complicated command. ... when I installed Linux for the first time, ... (Who would of thunk of escaping to normal mode and inputing a ... I normally set EDITOR so I get the ...
    (Fedora)
  • Re: logon scripts
    ... happen if the machines have been running and the user is just logging on for ... the first time in the morning? ... command at the command line: ... Once in awhile the script will run> fine, other times it will flash by and not load any drive> mappings and once in awhile it will not run at all. ...
    (microsoft.public.win2000.group_policy)
  • Re: "RunAs" Command
    ... You add the switch to the shortcut target. ... The first time you run it, ... Enable and Use the "Run As" Command When Running Program ... 2004 Windows MVP "Winny" Award ...
    (microsoft.public.windowsxp.general)