I'm at a loss of what to do here. So I have this simple while loop meant to allow a user to re-input a number if they accidentally give an incorrect input. The issue is, if you input "2" it loops back again. I can't, for the life of me figure it out.
void Player::playerPick()
{
int selection = 0;
while (selection != (1 || 2))
{
cout << "Player 1 or Player 2 (Type [1] or [2])";
cin >> selection;
}
}
You wrote:
while (selection != (1 || 2))
This is "while selection is not one or two".
The actual correct English is "while selection is neither one nor two", and that's true in C++ also:
while (!(selection == 1 || selection == 2))
Or, simpler, "while selection is not one, and selection is not two":
while (selection != 1 && selection != 2)
The expression 1 || 2
1 || 2
evaluates to true
, so you wrote while (selection != true)
, which is the case for any non-zero value of selection
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.