deal with bad input cpp 1

deal with bad input cpp

catch (std::ios_base::failure& e)
	std::cin.clear(); //clear bad input flag
	std::cin.ignore(std::numeric_limits::max(), '\n'); //discard input

Here is what the above code is Doing:
1. The try block attempts to read an integer from the user.
2. If the user enters a non-integer value, the iostream library will throw an exception of type ios_base::failure.
3. The catch block catches this exception and clears the error state of the cin object using cin.clear().
4. The cin.ignore() function is used to ignore the remaining characters in the input buffer.
5. The printf() function is used to print the error message.

Similar Posts