Codexing
Codexing

Reputation: 133

C++: Looping Menu Switch

I have an assignment where I have to have a menu which is somewhat working, it can't crash or exit if I type in the wrong input and it can't have an endless loop. Although my menu doesn't exits or crashes it goes in to an endless loop where if I type in anything but an integer. Here is my code.

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

The other menus.

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

Upvotes: 0

Views: 1715

Answers (1)

philipxy
philipxy

Reputation: 15118

When cin to an integer doesn't find one it sets an error flag and does not read from the input until the flag is cleared.

See this answer or this one.

Search on "cin infinite loop" and read the cin documentation.

Upvotes: 1

Related Questions