C++:循环菜单开关

C++: Looping Menu Switch

本文关键字:开关 菜单 循环 C++      更新时间:2023-10-16

我有一个作业,我必须有一个有点工作的菜单,如果我输入错误的输入,它不会崩溃或退出,它不能有一个无限循环。虽然我的菜单没有退出或崩溃,但它会进入一个无限循环,如果我输入除整数以外的任何内容。这是我的代码。

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 文档。