Question: C++ program, please include screenshot! thanks Common User Error A common user error is to input character data when numeric data is requested. This error
C++ program, please include screenshot! thanks
Common User Error
A common user error is to input character data when numeric data is requested. This error may cause a program to react unpredictably.
Write a function called getnum() that accepts a value, verifies it is numeric, and returns it. If the user inputs a non-numeric input, the program should recover from the error status and throw an exception.
In the main function, write a try...catch clause which uses the getnum() function to obtain an integer value. If the user inputs character data, the program should use the catch to display an error message. The program should loop until a numeric response is given by the user.
Hints:
If the program executes a cin statement with an integer variable and the user inputs character data, the statement evaluates as false. So, an easy check for numeric data might use the if statement: if (cin >> num)... This check will detect invalid character data. This check will not detect if a decimal value was entered.
Research the cin.clear() and cin.ignore() statements. If an input results in an error state, the programmer must reset the input status and clear out the buffer before asking for additional input.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
