C++:循环菜单切换

C++: Looping Menu Switch

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

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;
    }
}

当找不到一个整数的 cin 时,它会设置一个错误标志,并且在标志被清除之前不会从输入中读取。

this answer or this one

搜索 "cin infinite loop" 并阅读 cin 文档。