陷入无限 While 循环,怎么办

Stuck in Infinite While Loop, what to do

这里不接受任何回答:

cout << "Restart yes or no: ";
cin >> retry;
while (retry != "yes" or retry != "no"){
    cout << "Restart yes or no: ";
    cin >> retry;
    system("cls");
}

如果有人可以提供alternative/fix,将不胜感激。

您的代码有 retry != yes or retry != no。此条件是重言式,因此将始终计算为真。

将您的代码编辑为:

cout << "Restart yes or no: ";
cin >> retry;
while (retry != "no"){
    cout << "Restart yes or no: ";
    cin >> retry;
    system("cls");
}

如果您打算循环直到收到 yesno,那么 while 循环应该 运行 直到输入的字符串不等于任何一个。您打算使用逻辑 AND 代替 OR。代码应为:

while (retry != "yes" && retry != "no"){

每个 字符串都不同于"yes""no"。只要字符串不同于 both "yes""no" 就循环 - 这意味着使用逻辑 "and" 运算符,而不是 "or" 运算符:

while (retry != "yes" && retry != "no") {