我有以下代碼,並試圖找出如何通過點擊任意鍵返回到主菜單的選項,只要不選擇退出選項。我假設這個while循環是如何完成的,但是當我執行代碼時,它將在1次迭代後終止。我只是在學習C++,所以我不太清楚如何瀏覽這個問題。返回主菜單(嵌套列表,C++)
//Runs a program with a menu that the user can navigate through different options with via text input
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char userinp;
cout<<"Here is the menu:" << endl;
cout<<"Help(H) addIntegers(A) subDoubles(D) Quit(Q)";
cin >> userinp;
userinp = tolower(userinp);
int count = 1;
while (count == 1)
{
if (userinp == 'h')
{
cout <<"This is the help menu. Upon returning to the main menu, input A or a to add 2 intergers." << endl;
cout <<"Input D or d to subtract 2 doubles. Input Q or q to quit.";
count = 1;
return count;
}
else if (userinp == 'a')
{
int a, b, result;
cout <<"Enter two integers:";
cin >> a >> b;
result = a + b;
cout << "The sum of " << a << " + " << b << " = " << result;
count = 1;
return count;
}
else if (userinp == 'd')
{
double a, b, result;
cout <<"Enter two integers:";
cin >> a >> b;
result = a - b;
cout << "The difference of " << a << " - " << b << " = " << result;
count = 1;
return count;
}
else if (userinp == 'q')
{
exit(0);
}
else
{
cout <<"Please input a valid character to navigate the menu - input the letter h for the help menu";
cout << "Press any key to continue";
count = 1;
return count;
}
}
}
所有代碼的流向'return',除了一個 - 這確實一個'exit'。但是''''在'main'中被調用時'return'和'exit'是一樣的。您可能想要跳到下一個循環迭代:「繼續」。 (編輯:如果你有很長的'if's列表,你甚至不需要那個。) – usr2564301
有沒有一種方法可以在返回主菜單之前提示用戶敲一下鍵? – Sean