Possible duplicate:
Why is this cin reading jammed?
I overloaded the istream operator ( istream &operator>>... ) and it takes a point in the format:
(<x-coordinate>,<y-coordinate>)
I want to check this several (10) times, and therefore wrote:
for (int i = 0; i < 10; i++) { cin >> a; if (!cin.fail()) { cout << a << endl; } else { cout << "Invalid input!" << endl; cin.clear(); } }
EDIT:
Now I have the following code:
for (int i = 0; i < 10; i++) { cin >> a; if (!cin.fail()) { cout << a << endl; } else { cout << "Invalid input!" << endl; cin.clear(); while (!cin.eof()) { cin.ignore(); } cin.ignore(); } }
Rejection was proposed by Cthulhu. However, the problem is that cin still outputs "Invalid input!" after executing the code above:
(3,3) <-- input (3,3) <-- output Invalid output! <-- second output
Is there a way that I can clear what is in cin?
source share