What's this for?
Parsing input is a really advanced programming skill because you have no idea what the user will enter or when the data entry is finished.
Add to this the problem of cin >> data where an error occurs, like trying to put an A into an int. This causes the input stream to go into a fail state. Every subsequent cin will check the fail state and if it is turned on the cin fails. The effect is that on the first error it looks like every cin in the rest of the program is disabled since they all fail. Worse, when this happens you can't stop it.
For beginners, I suggest you enter exactly what your progrsm requires and nothing else. And, I would do this after you have completely tested your code by using hardcoded values. This way you can stay focused on the data entry parse and not be tripped up by logic errors in the rest of the program.
Just to cin >> to an int you need code like:
- bool GetInt(int* data)
-
{
-
cin >> *data;
-
if (cin.good())
-
{
-
return true;
-
}
-
else
-
{
-
cin.clear();
-
return false;
-
}
-
}
Even here you can see that you can't flush an input buffer because a)you don't know if the user has finished entering data, and b) your program is suspended while the user enters data. That means the input buffer is not yours so you can't flush it. All you can do is clear the error flag.
The only action you can take is if the error flag is set you can display a message and then terminate your program.
---unless you write your own version of cin which is what actual developers do.