How do I use standard tools to verify the input of an integer from the console? I want the program to report an error if something like this is entered:
123.3
1223aaw
123 122
Those. only positive and negative integers are allowed.
I tried it myself:
while (!(cin >> k)) { if (cin.fail()) cout << "error" << endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); }