How to Output Error When Input Isn't a Number in C++? Last Updated : 19 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, when taking user input, we may need to validate that it is a number and output an error if it is not. In this article, we will learn how to output an error when the input isn't a number in C++. For Example, Input:Enter a Number: GOutput:Error: That was not a number.Output Error When Input Isn't a Number in C++The std::cin automatically deduces the type of data that is to be extracted from the input and puts it into the specified variable. We can use the (!) NOT operator with cin which is overloaded to check the state of the cin. The state of std::cin is set to bad when the input data type does not match the variable type. We can use this property to check whether the given data is a number or not. ApproachTo output an error when the input entered by the user is not a number do the following: First, try to read the input using std::cin and check for input failure.If an error is detected, use std::cin.clear() to clear the error state, and std::cin.ignore() to ignore the rest of the input line.Finally, output an error message if an error is detected. C++ Program to Output Error When Input isn't NumberThe below example demonstrates how we can check and output an error when the input isn't a number in C++. C++ // C++ program to Output Error When Input Isn't a Number #include <iostream> #include <limits> using namespace std; int main() { // Prompt the user to enter a number cout << "Please enter a number: "; int num; // Keep reading input until a valid number is entered while (!(cin >> num)) { // Clear the error state cin.clear(); // Ignore the rest of the incorrect input cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Prompt the user again cerr << "Error: That was not a number. Please " "enter a number: "; } // Output the entered number cout << "You entered the number: " << num << endl; return 0; } Output: Please enter a number: gError: That was not a number. Please enter a number: 5You entered the number: 5Time Complexity: O(1)Space Complexity: O(1) Comment More infoAdvertise with us Next Article How to Output Error When Input Isn't a Number in C++? A at52kvoq Follow Improve Article Tags : C++ Programs C++ cpp-input-output C++ Errors CPP Examples +1 More Practice Tags : CPP Similar Reads How to Read Multiple Numbers from a Single Line of Input in C++? In C++, when working with user inputs we often need to take input of multiple numbers from a user in a single line. In this article, we will learn how to read multiple numbers from a single line of input in C++. Example Input: 1 7 0 4 6 8 Output: Entered Number: 1, 7, 0, 4, 6, 8Take Multiple Numbers 2 min read How to Take Operator as Input in C++? Operators are symbols that specify some kind of operation. In C++, we sometimes need to take operators as user input mainly to perform mathematical operations. In this article, we will learn how to take operators as user input in C++. Operators as Input in C++To take operators (like +,-,*,/ etc) as 2 min read How to Get Error Message When ifstream Open Fails in C++? In C++, the std::ifstream class is used to open a file for input operations. It associates an input stream to the file. However, due to some reasons, it may be unable to open the requested file. In this article, we will learn how to show an error message when the ifstream class fails to open the fil 2 min read How to Ask User Input Until Correct Input is Received? User input is a common source of errors in C++ programs so itâs important to validate user input to make the program more reliable. In this article, we will discuss how to ask the user for input until valid input is received in our C++ program. For Example, Input: asdf Output: Incorrect Input. Pleas 2 min read How to Use the Not-Equal (!=) Operator in C++? The not-equal operator is a fundamental comparison operator in C++ represented by "!=". It is used for making decisions in programming and is hence called a conditional operator. In this article, we will discuss how to use the Not-Equal (!=) operator in C++. Not-Equal (!=) Operator in C++The not-equ 3 min read Like