If cin cannot translate what it finds into the appropriate variable, it enters a fail state. You can check for this. Here is some example code:
Code:
#include <iostream>
#define expletive "moron"
using namespace std;
int main () {
int n;
bool nisgood = false;
while (!nisgood) {
cin >> n;
if (cin.fail()) {
cout << "That was supposed to be a NUMBER, " << expletive << ", now try again";
//fflush(stdin); // Heh just kidding
while (getchar() != '\n') {}
cin.clear();
} else nisgood = true;
}
return 0;
}
You can of course make this more elegant by tidying the error checking method into a tighter loop.