Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的do while循环永远不会结束?C++_C++_Do While - Fatal编程技术网

为什么我的do while循环永远不会结束?C++

为什么我的do while循环永远不会结束?C++,c++,do-while,C++,Do While,菜单 开始用虚空函数写谚语 }while (wordCode != QUIT_MENU); return 0; } 您没有输入有效的选择。 请输入1或2 无论选择什么值,上面的文本都会不断重复。为什么要重复 void writeProverb (int wordCode) { //Fill in the body of the function to accomplish what is described above if (wordCode == 1) { cout <

菜单

开始用虚空函数写谚语

}while (wordCode != QUIT_MENU);

return 0;
}
您没有输入有效的选择。 请输入1或2

无论选择什么值,上面的文本都会不断重复。

为什么要重复

void writeProverb (int wordCode)
{
 //Fill in the body of the function to accomplish what is described above

if (wordCode == 1)
{
    cout << "Now is the time for all good men to come to the aid of their party." << endl;
}

if (wordCode == 2)
{
    cout << "Now is the time for all good men to come to aid the aid of their country." << endl;
}

}
用户输入:

while (wordCode >= 1 || wordCode <= 2)
无论用户输入什么数字,条件都不会变为假。

改变

           wordCode >= 1      wordCode <= 2            
1              True                True            -> True  || True -> True
-1             False               True            -> False || True -> True
2              True                True            -> True  || True -> True
3              True                False           -> True  || False -> True
999999         True                False           -> True  || False -> True
-99999         False               True            -> False || True -> True

你不希望那个部分循环;外部的do/while处理得很好。您只需要输出一次消息。

要添加到Marc B的答案中,我想您需要在wordCode==1 | | wordCode==2时更改,而wordCode>=1 | | wordCode=1&&wordCode如果为真,这有什么不同
while (wordCode >= 1 || wordCode <= 2)
           wordCode >= 1      wordCode <= 2            
1              True                True            -> True  || True -> True
-1             False               True            -> False || True -> True
2              True                True            -> True  || True -> True
3              True                False           -> True  || False -> True
999999         True                False           -> True  || False -> True
-99999         False               True            -> False || True -> True
while (wordCode >= 1 || wordCode <= 2)
if (wordCode == 1 || wordCode == 2)