Absorbed
October 10th, 2008, 10:48 PM
I'm making a little program that plays a game. During the game I use scanf to take integers, and that works fine. However, when I use scanf to take a char, to ask the user if they want to play again, the call just gets ignored. Here's the relevant code:
int main(void) {
int wanttoplay = 1;
int numWins = 0;
while(wanttoplay) {
numWins = game(numWins);
char choice;
printf("You've won %d times.\n", numWins);
printf("Do you want to play another game? (y/n)\n");
scanf("%c", &choice);
if(choice != 'y')
wanttoplay = 0;
}
printf("\n");
return 0;
}
Here's what happens when I finish playing a game:
You lost!
You've won 0 times.
Do you want to play another game? (y/n)
absorbed@absorbedscomputer:~$
It doesn't pause to let me type y or n. Help!
int main(void) {
int wanttoplay = 1;
int numWins = 0;
while(wanttoplay) {
numWins = game(numWins);
char choice;
printf("You've won %d times.\n", numWins);
printf("Do you want to play another game? (y/n)\n");
scanf("%c", &choice);
if(choice != 'y')
wanttoplay = 0;
}
printf("\n");
return 0;
}
Here's what happens when I finish playing a game:
You lost!
You've won 0 times.
Do you want to play another game? (y/n)
absorbed@absorbedscomputer:~$
It doesn't pause to let me type y or n. Help!