If an error occurs while reading from the stream, the error flag is set, and until you clear the error flags, there will be no more reading. That is why you get an infinite loop.
cin.clear();
In addition, it is incorrect to use the results of an input operation if you do not know if the reading was the first. You cannot make any assumptions about the meaning of iAuswahl . This is one of the most common mistakes made by newcomers using threads. Always check if the input operation was ok. This is easiest to do using operator>> in a boolean context:
if (cin >> some_obj) { // evaluates to true if it succeeded } else { // something went wrong }
And my mine, this line
while (iAuswahl != 1 && iAuswahl != 2 && iAuswahl != 3 && iAuswahl != 4 && iAuswahl != 5 && iAuswahl != 6 && iAuswahl != 7 && iAuswahl != 8 && iAuswahl != 9)
may be as follows:
while (iAuswahl < 1 || iAuswahl > 9)
The correct loop might look something like this:
while (true) { if ((cin >> iAuswahl) && (iAuswahl >= 1) && (iAuswahl <= 9)) break; std::cout << "error, try again\n"; cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); }
source share