I need to get an integer from input. I wrote the code:
int price;
srand((unsigned)time(NULL));
price = rand() % 1000 + 1;
int i;
cout << "Please guess the price(1--1000):" << endl;
cin >> i;
while (cin.fail()) {
cerr << "error"<<endl;
fflush(stdin);
//cin.clear();
//cin.ignore(numeric_limits<std::streamsize>::max());
//cin.get();
cin >> i;
}
I've tried many methods, but if I input a charactere
, the result is:
e
error
error
error
error
error
error
error
error
error
error
error
error
error
error
error
error
error
error
I'm so confused, thanks for your help.