I've run into a dilema when requesting a response from a user, and can't
figure out the proper method to use to get the results I am after.
What I have is a basic question asking two users if they wish to continue,
and a cin statement that gathers their response.
What kills the process I have, is I'd like to specifically make each user
enter only a lowercase 'y' or 'n' as their response. What I have works, but
it will accept any input from the user. Thus, it 'breaks' the program if a
user types "yes" or "no" or other non-revelant characters. I've tried it
with string, char, and combinations of int, but can't get anything working
properly. Would you be so kind as to point me in the right direction?
Here's the relevant code that I've managed to get working as long as they
type in only a 'y' or an 'n':
char playAgain;
cout << playerStats[playerTurn].playerName << " has won!" << endl;
playerStats[playerTurn].wins++; //Add one to player's total
wins.
cout << playerStats[1].playerName << ", would you like to play again
(y/n)? ";
cin >> playAgain;
if (playAgain == 'n')
finished = 1;
cout << playerStats[2].playerName << ", would you like to play again
(y/n)? ";
cin >> playAgain;
if (playAgain == 'n')
finished = 1;
if (finished != 1)
{
......
}
I did use: string playAgain; at one point, and was using: if (playAgain[0]
== 'n') { ..... } and while that would match the first character quite
nicely...any extraneous characters were taken as input for the 2nd
user...thus they weren't given a chance to respond.
Sorry about the bad formatting, don't know how to get it to show up correctly...any thoughts?
figure out the proper method to use to get the results I am after.
What I have is a basic question asking two users if they wish to continue,
and a cin statement that gathers their response.
What kills the process I have, is I'd like to specifically make each user
enter only a lowercase 'y' or 'n' as their response. What I have works, but
it will accept any input from the user. Thus, it 'breaks' the program if a
user types "yes" or "no" or other non-revelant characters. I've tried it
with string, char, and combinations of int, but can't get anything working
properly. Would you be so kind as to point me in the right direction?
Here's the relevant code that I've managed to get working as long as they
type in only a 'y' or an 'n':
char playAgain;
cout << playerStats[playerTurn].playerName << " has won!" << endl;
playerStats[playerTurn].wins++; //Add one to player's total
wins.
cout << playerStats[1].playerName << ", would you like to play again
(y/n)? ";
cin >> playAgain;
if (playAgain == 'n')
finished = 1;
cout << playerStats[2].playerName << ", would you like to play again
(y/n)? ";
cin >> playAgain;
if (playAgain == 'n')
finished = 1;
if (finished != 1)
{
......
}
I did use: string playAgain; at one point, and was using: if (playAgain[0]
== 'n') { ..... } and while that would match the first character quite
nicely...any extraneous characters were taken as input for the 2nd
user...thus they weren't given a chance to respond.
Sorry about the bad formatting, don't know how to get it to show up correctly...any thoughts?