I have this code which is supposed to fool-proof the program from users entering something else but an integer. I wrote this code based on several sources online but for some reason it doesn't work.
int cost;
cin >> cost;
if (!(cin>>cost)) {
cout << "Enter a number: ";
cin >> cost;
cin.ignore(10000, '\n');
}
The prompt which is supposed to show up when you enter an incorrect type doesn't appear and the program terminates. I've tried moving around and adding cin.ignore() to other places, I've also tried if(cin.fail()) with no success.
cin >> cost; if (!cin >> cost) ...?