我不知道如何處理錯誤的輸入。我做了一個嵌套的while回圈,因為如果我只使用If陳述句并且輸入錯誤,它會跳轉到第一個while回圈的開頭,而不是“你想繼續嗎?”。如果我使用嵌套的 while 回圈,即使滿足 bool 條件,它也不會完成。請幫忙。
Github 上的原始代碼
while((end2 == false))
{
cout << endl;
cout << "Do you wish to continue? (Y / N)" << endl;
char go_on;
cout << "Your choice: ";
cin >> go_on;
cout << endl;
if((go_on != 'Y') && (go_on != 'y') && (go_on != 'N') && (go_on != 'n'))
{
cout << "Invalide input! Choose between Y and N" << endl;
continue;
}
if((go_on == 'Y') || (go_on == 'y'))
{
end2 == true;
end == false;
}
if((go_on == 'N') || (go_on == 'n'))
{
end2 = true;
end == true;
}
}
uj5u.com熱心網友回復:
創建函式可能要簡單得多
bool check_continue() {
while(true) {
// get your input
if((go_on == 'Y') || (go_on == 'y'))
return true;
if((go_on == 'N') || (go_on == 'n'))
return false;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/457291.html
標籤:C
