[英]While Loop Not Recognizing Input
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.
因此,我有一个简单的while循环,意在允许用户在意外输入错误的情况下重新输入数字。 The issue is, if you input "2" it loops back again.
问题是,如果您输入“ 2”,它将再次循环返回。 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: 实际正确的英语是“而选择既不是一,也不是两个”,在C ++中也是如此:
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
1 || 2
evaluates to true
, so you wrote while (selection != true)
, which is the case for any non-zero value of selection
. 1 || 2
计算结果为true
,所以你写的while (selection != true)
这是任何非零值的情况下selection
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.