Data Type Exception Handling

I am writing a program that requires the use of exception handling for user input. The user is required to enter a positive integer. I accounted for the exception of negative integers, but how can I account for when the user enters a character instead?

Like if the user typed 'a' instead of '3'.

If someone could tell me how to implement the exception for this case with the use of try/catch blocks, that would be very helpful. Thank you.
Hi Dhaneku,

I'm not sure that cin throws an exception on bad input, unless you explicitly tell it to do so. In any case, I'm not sure an exception is the best solution, as it adds unnecessary complications to the age-old problem.

cin, upon invalid input, sets its failbit to true(1). It's easier to detect cin failure, deal with the bad input and ask for valid input (again).

Stackoverflow has a great explaination on both solutions (setting cin to throw, and using a while loop) http://stackoverflow.com/questions/19696442/how-to-catch-invalid-input-in-c
Topic archived. No new replies allowed.