C ++:循环菜单开关

时间:2015-01-03 22:20:55

标签: c++ menu switch-statement infinite-loop cin

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

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

1 个答案:

答案 0 :(得分:1)

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

请参阅this answerthis one

搜索&#34; cin无限循环&#34;并阅读cin文档。