using cin.clear() to remove the fail state in an exception

For the input line

Mary Beth 19

the first

cin >> inputName;

will read Mary into inputName.


cin >> age;

attempts to parse Beth as an integer, but it will fail and thrown an exception. However, and here’s the important part: The input isn’t discarded! Instead the input read pointer is unmodified, it will still be “pointing” to Beth.

You clear the error and the loop continues as normal, coming back to

cin >> inputName;

which will then continue reading the Beth into inputName, loosing the old value Mary.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top