我遇到了错误,他们说 a、q、h 等不是声明的变量,但我不知道如何解决这个问题。这是我的代码示例。
我也收到一个错误,它无法识别 while 循环内的 userinp,但我不确定如何在不声明全局变量的情况下将变量存储从 while 循环外部转移到 while 循环内部,我想要避免。
最后一个问题:我希望能够在每个菜单项之后让任何键立即返回主菜单,但我不确定该使用什么。
任何帮助是极大的赞赏!
//Runs a program with a menu that the user can navigate through different options with via text input
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <ctype.h>
#include <string>
using namespace std;
int main()
{
int userinp;
cout<<"Here is the menu:" << endl;
cout<<"Help(H) addIntegers(A) subDoubles(D) Quit(Q)";
cin >> userinp;
userinp = tolower(userinp);
while userinp != q //loop while no q is input
{
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.";
}
if userinp == a
{
int a, b, result;
cout <<"Enter two integers:";
cin << a << b;
result = a + b;
cout << "The sum of " << a << " + " << b << " = " << result;
}
}
}