還好沒有退出,所以我是新來的全做while循環的事情,我試圖做一個主菜單,這裏是我下面的代碼:做while循環嵌套if語句在C++中
int main()
{
int choice;
char sure;
bool quit = false;
char ctrl;
do
{
cout << "Main Menu." << endl
<< "1. New Game." << endl
<< "2. Load Game." << endl
<< "3. Exit." << endl
<< "Your choice: ";
cin >> choice;
if (choice == 1)
{
cout << "Are you sure you wish to start a new game? (Y/N) ";
cin >> sure;
if (sure != 'N' || sure != 'n')
{
ctrl = 'a';
quit = true;
}
}
else if (choice == 2)
{
ctrl = 'b';
quit = true;
}
else
quit = true;
}
}
while (quit == true);
if (ctrl = 'a')
cout << "New Game." << endl;
else if (ctrl = 'b')
cout << "Load Game." << endl;
else
cout << "Goodbye." << endl;
return 0;
}
有幾個getchar()拋出那裏。但唯一的問題是,你可能會發現,在我做完所有事情之後,它只是再次重新啓動,而不是退出循環。代碼中有什麼問題?
感謝
¤主要問題是您沒有解決上次發佈時我評論的內容。當你不修復它時,它會*繼續*是錯誤的。爲了讓你有機會走上正軌,用'while(!quit)'替換'while(quit == true)',並開始思考對於其餘代碼的意義。乾杯&hth。, –
不要只用正確的代碼替換錯誤的代碼。答案和問題本身不再有任何意義。 – Xeo