【发布时间】:2015-01-03 22:20:54
【问题描述】:
我有一个任务,我必须有一个可以正常工作的菜单,如果我输入错误的输入,它就不会崩溃或退出,并且它不能有无限循环。虽然我的菜单没有退出或崩溃,但它会进入一个无限循环,如果我输入除整数以外的任何内容。这是我的代码。
void mainMenu()
{
int option;
cout << "\t\t\t***** Project: Algorithms *****\n\n\n";
cout << "Enter your selection.\n\n";
cout << "1\tSearches.\n";
cout << "2\tCalculations and negations.\n";
cout << "3\tCopying.\n";
cout << "4\tExit 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 << "\t\t\t***** Search *****\n\n\n";
cout << "Enter your selection.\n\n";
cout << "1\tSearch for a element with find.\n";
cout << "2\tSearch for an element with binary search.\n";
cout << "3\tSearch for pair elements.\n";
cout << "4\tBack 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 << "\t\t\t***** Calculate or Negate *****\n\n\n";
cout << "Enter your selection.\n\n";
cout << "1\tCalculate the total sum of all elements in the vector.\n";
cout << "2\tNegate all elements in the vector.\n";
cout << "3\tBack 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 << "\t\t\t***** Copy *****\n\n\n";
cout << "Enter your selection.\n\n";
cout << "1\tCopy to list.\n";
cout << "2\tCopy to file.\n";
cout << "3\tBack 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;
}
}
【问题讨论】:
-
这不是循环问题,但是您对 mainMenu() 等的所有嵌套调用都会导致程序从对其的调用中对其进行越来越多的调用,而不会返回 i> 从任何使用越来越多的内存,直到你退出()。
-
感谢您的回答(我还是编程新手。),问题是我试图让它变得万无一失,这样当有人输入一个不属于开关的数字时如果它说错误并返回主菜单。
-
查看我的答案:了解 cin 的工作原理,以便在 cin(或您选择的任何方法)找不到整数时执行所需的操作。关于“事情是......”:你想说什么?这是一个目标。这不是做没有完成的事情的理由。
标签: c++ menu switch-statement infinite-loop cin