1

i try to do a simple menu using switch. I also want to do a check if the user made a valid input (Only int from 1 to 4). Entering -4 or 44 is working fine with this check. But if i enter something like "w" it gives me a infinite loop. I'm guessing i need another if / else with if (!cin) blabla else go ahead with switch. But i'm not sure how i do that the else is starting the switch.

 int menu() {
        int enter;
        bool exit = false;
        do {
            cout << "Wie soll angefangen werden: " << endl; //Enter your choice
            cout << "1 - Spiel starten" << endl; // do game();
            cout << "2 - Highscore " << endl; //do score();
            cout << "3 - Quiz starten " << endl; //do quiz();
            cout << "4 - Ende " << endl; //end the programm

        cin >> enter; 

        switch (enter) {
            case 1:
                game();
                break;
            case 2:
                score();
                break;
            case 3:
                showQuizDialog();
                break;
            case 4:
                exit = true;
                break;
            default:
                cout << "Keine gültige Eingabe, nochmal: " << endl; //invalid input, again
                void flushCin();
        } //end of switch
    } while (exit == false); 

}//end of menu();
AnnoyedGuy
  • 111
  • 1
  • 11
  • what is `flushCin`? You shouldn't need to flush the stream as you've already read from it. – Mgetz Nov 15 '13 at 13:27
  • Try changing `int enter` to `char enter` and `case 1:` to `case '1':` – Shubham Nov 15 '13 at 13:28
  • Two points of style. First: there's no point in comparing a `bool` with a constant; the final condition should be just `while ( !exit )`. And second, you really should add a `break;` in the default case as well. – James Kanze Nov 15 '13 at 14:02
  • Possible duplicate of [Infinite loop with cin when typing string while a number is expected](https://stackoverflow.com/questions/5864540/infinite-loop-with-cin-when-typing-string-while-a-number-is-expected) – melpomene Jan 10 '19 at 23:22

2 Answers2

11

It's because the input is trying to get an integer. When the input is not an integer, the input is left in the buffer, so next time around in the loop the same input is still there.

Also, you are not calling the flushCin function in the default case, you are declaring it. You might want to remove the void keyword. I guess it does the correct thing? (I.e. calling std::cin.ignore() and std::cin::clear().)

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Read into a string and try to convert to int:

#include <sstream>
#include <string>
using namespace std;

int menu() {
        int enter;
        string str;


        bool exit = false;
        do {
            cout << "Wie soll angefangen werden: " << endl; //Enter your choice
            cout << "1 - Spiel starten" << endl; // do game();
            cout << "2 - Highscore " << endl; //do score();
            cout << "3 - Quiz starten " << endl; //do quiz();
            cout << "4 - Ende " << endl; //end the programm

        cin >> str; 
        istringstream buffer(str);
        buffer >> enter;

        switch (enter) {
            case 1:
                game();
                break;
            case 2:
                score();
                break;
            case 3:
                showQuizDialog();
                break;
            case 4:
                exit = true;
                break;
            default:
                cout << "Keine gültige Eingabe, nochmal: " << endl; //invalid input, again
                void flushCin();
        } //end of switch
    } while (exit == false);

    return enter;

}//end of menu();

If entering other things than numbers into an int value directly this might not fit into the reserved space of an int and can result in funny behaviour. So just read into a string first and then interpret it.

Beachwalker
  • 7,685
  • 6
  • 52
  • 94
  • you don't need the `stringstream`, [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) would do the job just fine. – Mgetz Nov 15 '13 at 13:37
  • @Mgetz right, there are thousand ways to do the same thing in c++ ;-) – Beachwalker Nov 15 '13 at 13:39
  • 1
    Agreed, however in this case because the OP is only reading `int` there is no need for something so heavy weight, if they were doing complex formatting... that would be different. – Mgetz Nov 15 '13 at 13:40