0

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;
    }
}
philipxy
  • 14,867
  • 6
  • 39
  • 83
Codexing
  • 133
  • 1
  • 11
  • https://stackoverflow.com/help/mcve – philipxy Jan 03 '15 at 23:49
  • This isn't the loop problem, but all your nested calls to mainMenu() etc cause the program to make more and more calls to it from within calls to it without ever *returning* from any using more and more memory until you exit(). – philipxy Jan 04 '15 at 00:12
  • possible duplicate of [cin for an int inputing a char causes Loop that is supposed to check input to go wild](http://stackoverflow.com/questions/18400620/cin-for-an-int-inputing-a-char-causes-loop-that-is-supposed-to-check-input-to-go) – philipxy Jan 04 '15 at 00:14
  • Thanks for the answer(I'm still new to programming.), the thing is that I'm trying to get it to be as foolproof so that when someone entered a number not part of the switch cases it said error and returned to main menu. – Codexing Jan 04 '15 at 00:27
  • See my answer: Learn how cin works so that you do what you need to if cin (or whatever method you choose) can't find an integer. Re "the thing is...": What are you trying to say? That's a goal. It isn't a reason to do something that doesn't accomplish it. – philipxy Jan 04 '15 at 00:43
  • You are not *returning to main menu*, you are *calling main menu again*. You need a loop calling or within mainMenu. Other routines should `return` to where they came from. – philipxy Jan 04 '15 at 00:59

1 Answers1

1

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.

philipxy
  • 14,867
  • 6
  • 39
  • 83
  • It works, thank you so much. Though I wonder if there is a way to delay it so that the error message can get out before it stops the stream. – Codexing Jan 04 '15 at 00:40
  • it is done, I just wanted to know if there is a way to it without having to make a whole new post. – Codexing Jan 04 '15 at 00:45
  • There are two streams. If you want pending characters on the output stream to appear before cin flushes it, flush it yourself, as you do elsewhere. – philipxy Jan 04 '15 at 00:46