C++ 如何制作用户可以选择的菜单
我正在尝试制作一个菜单,用户可以从选项列表中进行选择。在他们选择其中一个选项后,它将显示一个C++ 如何制作用户可以选择的菜单,c++,C++,我正在尝试制作一个菜单,用户可以从选项列表中进行选择。在他们选择其中一个选项后,它将显示一个coutUsado while控制流块,并检查使程序结束的特定输入 例如,您可以按如下方式更改程序的主结构: int flag = 1; char c = 'a'; do { cout << "SHOW THE MENU " << endl; cout << "press 'x' to exit" << endl; cin>&g
coutUsado while
控制流块,并检查使程序结束的特定输入
例如,您可以按如下方式更改程序的主结构:
int flag = 1;
char c = 'a';
do {
cout << "SHOW THE MENU " << endl;
cout << "press 'x' to exit" << endl;
cin>>c;
if(c == '1')
{
cout << "doing 1" << endl;
}
else if (c == '2')
{
cout << "doing 2" << endl;
}
} while(c != 'x');
while (option != -1) // put whatever your loop exit condition is here
{
cout << "\n\n\nEnter the number of one of the following and I will explain!\n";
cout << "1.integer 2.boolian 3.floats 4.doubles 5.character";
cout << "\n\n[when you are done type 'done' to continue]\n\n";
cin >> option;
if (option == 1)
{
cout << "\nInteger is the variable abbreviated as 'int' this allows C++ to only";
cout<<"\nreadwhole and real numbers \n\n";
}
}
int标志=1;
字符c='a';
做{
cout您需要在设置的while
循环中使用所有菜单逻辑。您当前的while(10)
条件应该会导致无限循环;这可能是当前版本无法工作的原因。我会尝试修改while
循环,如下所示:
int flag = 1;
char c = 'a';
do {
cout << "SHOW THE MENU " << endl;
cout << "press 'x' to exit" << endl;
cin>>c;
if(c == '1')
{
cout << "doing 1" << endl;
}
else if (c == '2')
{
cout << "doing 2" << endl;
}
} while(c != 'x');
while (option != -1) // put whatever your loop exit condition is here
{
cout << "\n\n\nEnter the number of one of the following and I will explain!\n";
cout << "1.integer 2.boolian 3.floats 4.doubles 5.character";
cout << "\n\n[when you are done type 'done' to continue]\n\n";
cin >> option;
if (option == 1)
{
cout << "\nInteger is the variable abbreviated as 'int' this allows C++ to only";
cout<<"\nreadwhole and real numbers \n\n";
}
}
while(option!=-1)//将循环退出条件放在这里
{
如果要支持“done”
,请将输入保存为字符串,然后使用将其转换为int
。使用switch
而不是一堆if
s和while(true)
继续,直到使用break
关键字
std::string in;
int op = 0;
while (true)
{
// PRINT OPTIONS
// GET INPUT
if (in == "done") break;
switch (std::stoi(in))
{
case 1:
// OPTION 1
break;
case 2:
// OPTION 2
break;
default:
// INVALID OPTION
break;
}
}
“但这不起作用”有点含糊?!?…而(10)
?没有。而且,选项不能“完成”
因为它是一个int
。很明显,我希望他/她在理解这个问题时表现出一点努力。我不这么认为,问题中代码的整体质量说明了在寻找解决方案时所花费的全部努力。看看这个例子…if(option=1);