Try this:
try{
cin >> input;
if (cin.good()) {
if(input > 11 || input < 0) {
cout << "Under 10 you idiot!" << endl;
ask();
} else {
checkAnswer(input);
}
} else {
cout << "Input a number!" << endl;
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '
');
ask();
}
}catch(exception e){
cout << "An unexpected error occurred!" << endl;
ask();
}
and don't forget to use this at the beginning: #include <climits>
The cin.ignore(std::numeric_limits<std::streamsize>::max(), '
');
line will ignore everything until the next int number.. Therefore it will not loop anymore..
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…