Character input leading to default of switch statement always
From: Phoe6 (orsenthil_at_gmail.com)
Date: 02/27/05
- Next message: James Dennett: "Re: Character input leading to default of switch statement always"
- Previous message: Francis Glassborow: "Re: Problem with operator*()"
- Next in thread: James Dennett: "Re: Character input leading to default of switch statement always"
- Reply: James Dennett: "Re: Character input leading to default of switch statement always"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: James Dennett: "Re: Character input leading to default of switch statement always"
- Previous message: Francis Glassborow: "Re: Problem with operator*()"
- Next in thread: James Dennett: "Re: Character input leading to default of switch statement always"
- Reply: James Dennett: "Re: Character input leading to default of switch statement always"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|