I can’t understand why this gets into the loop after entering non-int. I tried cin.flush (), which does not seem to exist, cin.clear (), which seems to work, even cin.sync () after reading someone else a message about it, works, but it doesn't seem , has the meaning. Also tried cin.bad ().
Thanks so much for any help
Enter the first number: f Sorry, I do not think the number?
Enter the first number: Sorry, I do not think the number?
Enter the first number: Sorry, I do not think the number?
Enter the first number: Sorry, I do not think the number?
Enter the first number: Sorry, I do not think the number? Sorry, you will not try anymore. Press any key to continue.,.
#include <iostream>
using namespace std;
int main(){
int entry;
int attempts = 1;
int result;
while(attempts <= 5) {
try {
cout << "\n\nPlease enter the first number: ";
cin >> entry;
if (cin.fail())
throw "Sorry, I don't think that a number?";
if (entry < 0)
throw "Sorry, no negative numbers. Try something else? ";
cout << "\nNow the second number: ";
cin >> entry;
cin.clear();
cin.get();
}
catch (char* error) {
cout << error;
attempts++;
}
}
if (attempts > 5)
cout << "Sorry, you don\'t get any more tries.\n";
system("pause");
return 0;
}
source
share