How to Use cin.fail() Method in C++? Last Updated : 28 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the cin.fail() method is a part of <iostream> library that is used to check whether the previous input operation has succeeded or not by validating the user input. In this article, we will learn how to use cin.fail() method in C++. Example: Input: Enter an integer: aOutput: Invalid Input. Please Enter an Integer. C++ cin.fail() FunctionWe can use the cin.fail() method to check if the last input operation was successful or not. This function returns true if the last cin command failed and false otherwise. So, it can be used to validate user input and ensure that it meets certain criteria. Syntax to Use cin.fail() Methodif (cin.fail()) { // The last input operation failed} else { // The last input operation succeeded}C++ Program to Use cin.fail() Method The below example demonstrates how to use the cin.fail() method in C++. C++ // C++ program to use cin.fail() method #include <iostream> #include <limits> using namespace std; int main() { // Declare and initialize integer variables. int i = 0, j = 0; // Infinite loop to continually ask for input until a // valid integer is entered. while (true) { // Ask the user to enter int value. cout << "Enter an Integer: " << endl; i++; // Read input from the user. cin >> j; // Check if the input operation failed (i.e., input // was not an integer). if (cin.fail()) { // Clear the error flags on the input stream. cin.clear(); // leave the rest of the line cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Ask the user to enter a valid int number only cout << "Wrong input, please enter a number: "; } else { // Print the valid integer entered by the user. cout << "Integer " << i << ": " << j << endl; } } return 0; } Output Enter an Integer: 5Integer 1: 5Enter an Integer: MWrong input, please enter a number: Enter an Integer: 4Integer 3: 4....Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Use cin.fail() Method in C++? P pantharshx9d9 Follow Improve Article Tags : C++ Programs C++ cpp-input-output CPP Examples misc-cpp +1 More Practice Tags : CPP Similar Reads How to Use the ignore() Function in C++? In C++, the ignore() function is a part of std::basic_istream that is used to discard the characters in the stream until the given delimiter(including it) is reached and then extracts the left-out remainder. In this article, we will learn how to use the ignore() function in C++. C++ ignore() Functio 2 min read How to use errno in C++? In C++ errno is a preprocessor macro that is used for error indication and reporting errors that occur during a function call. It contains error codes so if a call to a function fails somehow then the errno is set to a value that corresponds to the error. errno is defined in the header file <cerr 4 min read How to Redirect cin and cout to Files in C++? In C++, we often ask for user input and read that input using the cin command and for displaying output on the screen we use the cout command. But we can also redirect the input and output of the cin and cout to the file stream of our choice. In this article, we will look at how to redirect the cin 2 min read How to Throw an Exception in C++? In C++, exception handling is a mechanism that allows us to handle runtime errors and exceptions are objects that represent an error that occurs during the execution of a program. In this article, we will learn how to throw an exception in C++. Throw a C++ ExceptionThrowing an exception means sendin 2 min read How to Use the Try and Catch Blocks in C++? In C++, the try and catch blocks are used as a part of the exception handling mechanism which allows us to handle runtime errors. If the exceptions are not handled properly then the flow of the program is interrupted and the execution might fail. In this article, we will learn how to use try and cat 2 min read How Should I Use FormatMessage() in C++? In C++, the FormatMessage() is a function used for formatting error messages returned by the system that is particularly useful for retrieving system error messages and converting them into a user-readable format. In this article, we will learn how we should use the FormatMessage function properly i 3 min read How to Take Multiple Input from User in C++? In C++, we use cin when we want to take input from the user. We often also need to take more than one input at a time. In this article, we will learn how to take multiple inputs in C++. Take Multiple Inputs from a User in C++To take multiple inputs from users, we can repeatedly use the std::cin usin 2 min read How to Throw a Custom Exception in C++? In C++, exception handling is done by throwing an exception in a try block and catching it in the catch block. We generally throw the built-in exceptions provided in the <exception> header but we can also create our own custom exceptions.In this article, we will discuss how to throw a custom e 2 min read How to Delete a File in C++? C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the 2 min read How to Handle Incorrect Values in a Constructor? In C++, constructors are used to construct and initialize the object of its class. If incorrect values are given to the constructor, then it may cause inconsistency in a program. In this article, we will learn how to handle incorrect values in a constructor in C++ Handling Incorrect Arguments for Co 2 min read Like