P
Phoe6
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
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