C++:循环菜单开关
C++: Looping Menu Switch
我有一个作业,我必须有一个有点工作的菜单,如果我输入错误的输入,它不会崩溃或退出,它不能有一个无限循环。虽然我的菜单没有退出或崩溃,但它会进入一个无限循环,如果我输入除整数以外的任何内容。这是我的代码。
void mainMenu()
{
int option;
cout << "ttt***** Project: Algorithms *****nnn";
cout << "Enter your selection.nn";
cout << "1tSearches.n";
cout << "2tCalculations and negations.n";
cout << "3tCopying.n";
cout << "4tExit the program.n";
cout << "Please enter the menu next to each option.n> " << flush;
cin >> option;
switch (option)
{
case 1: cout << "Yes!";
system("cls");
searchMenu();
break;
case 2: cout << "Yes!";
system("cls");
Calc_NegateMenu();
break;
case 3: cout << "Yes!";
system("cls");
copyMenu();
break;
case 4: cout << "Yes!";
exit(0);
break;
default: cout << "ERROR! Invalid input!";
system("cls");
mainMenu();
break;
}
}
其他菜单。
void searchMenu()
{
int option;
cout << "ttt***** Search *****nnn";
cout << "Enter your selection.nn";
cout << "1tSearch for a element with find.n";
cout << "2tSearch for an element with binary search.n";
cout << "3tSearch for pair elements.n";
cout << "4tBack to the main menu.n";
cout << "Please enter the menu next to each option.n> " << flush;
cin >> option;
switch (option)
{
case 1: cout << "Yes!";
system("cls");
searchMenu();
break;
case 2: cout << "Yes!";
system("cls");
Calc_NegateMenu();
break;
case 3: cout << "Yes!";
system("cls");
copyMenu();
break;
case 4: cout << "Yes!";
system("cls");
copyMenu();
break;
default: cout << "ERROR! Invalid input!";
system("cls");
mainMenu();
break;
}
}
void Calc_NegateMenu()
{
int option;
cout << "ttt***** Calculate or Negate *****nnn";
cout << "Enter your selection.nn";
cout << "1tCalculate the total sum of all elements in the vector.n";
cout << "2tNegate all elements in the vector.n";
cout << "3tBack to the main menu.n";
cout << "Please enter the menu next to each option.n> " << flush;
cin >> option;
switch (option)
{
case 1: cout << "Yes!";
system("cls");
searchMenu();
break;
case 2: cout << "Yes!";
system("cls");
Calc_NegateMenu();
break;
case 3: cout << "Yes!";
system("cls");
mainMenu();
break;
default: cout << "ERROR! Invalid input!";
system("cls");
mainMenu();
break;
}
}
void copyMenu()
{
int option;
cout << "ttt***** Copy *****nnn";
cout << "Enter your selection.nn";
cout << "1tCopy to list.n";
cout << "2tCopy to file.n";
cout << "3tBack to the main menu.n";
cout << "Please enter the menu next to each option.n> " << flush;
cin >> option;
switch (option)
{
case 1: cout << "Yes!";
system("cls");
searchMenu();
break;
case 2: cout << "Yes!";
system("cls");
Calc_NegateMenu();
break;
case 3: cout << "Yes!";
system("cls");
mainMenu();
break;
default: cout << "ERROR! Invalid input!";
system("cls");
mainMenu();
break;
}
}
当 cin 到整数找不到时,它会设置一个错误标志,并且在清除该标志之前不会从输入中读取。
看到这个答案或这个答案。
搜索"cin 无限循环"并阅读 cin 文档。
相关文章:
- 既然存在危险,为什么项目要使用-I include开关
- 无法获取菜单选择以运行函数.C++
- 为什么这个音频包络不能通过开关的情况?
- 有人知道为什么在开关中使用stoi函数会返回恒定的错误吗
- C 和 C++ 中开关语句的案例标签的常量值,但显示不同的行为
- 在 c++ 中在开关情况下使用和不使用"break"时的不同输出
- 为什么我的开关/机箱在使用枚举时默认?
- 为什么布尔开关语句有编译器警告?
- 如何使用"equal to"以外的评估编写开关语句
- 为什么开关的优化方式与 c/c++ 中的链接不同?
- 无法找到简单的开关大小写枚举错误
- 未达到的情况会影响开关外壳性能
- C++ 中的图形菜单
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?
- 我的开关菜单在一段时间循环中出现问题
- 新的C ,模块和开关菜单
- 如何在不使用开关大小写的情况下用C++编写菜单驱动的程序
- C++:循环菜单开关
- 为什么我的菜单不起作用 c++ 大小写开关
- c++中的滚动菜单?如何改变它的开关