Possible Duplicate:
Why is this cin reading jammed?
I overloaded the istream operator (istream &operator>>...) and it takes in a Point of the format:
(<x-coordinate>,<y-coordinate>)
I want to test this out multiple (10) times, and so have written:
for (int i = 0; i < 10; i++) {
cin >> a;
if (!cin.fail()) { cout << a << endl; }
else { cout << "Invalid input!" << endl; cin.clear(); }
}
EDIT:
I now 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();
}
}
The ignore was suggested by Cthulhu. However, the problem is cin still outputs “Invalid input!” after running through 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?
cin.clear()does not empty the buffer, it resets error flags on the stream.You then have to call cin.ignore
Numeric Limits