In order to figure out whether an input operation from the standard input (std::cin) has failed, use the C++ function std::cin.fail function. It is usually used to determine whether an input operation was successful after it has been performed.
(std::ios::failbit, std::ios::badbit, std::ios::eofbit) Input State Flags:
- The std::ios::failbit flag is set on the stream (std::cin in this example) when an input operation fails.
- A major issue with the stream (such as a memory allocation failure) sets the std::ios::badbit flag.
- When the input stream comes to an end, the std::ios::eofbit flag is set.
- This function is a member of the std::basic_ios class template. If the failbit or badbit is set on the std::cin stream, it returns true.
- It is usually used to determine whether an input operation was successful after it has been performed.
- An input operation is considered to have failed if std::cin.fail returns true (e.g., due to invalid input).
- The stream's error status flags (failbit, badbit, and eofbit) are set in the event of an input operation failure, which may stop additional input operations.
- The error status flags are cleared by std::cin.clear, enabling the execution of additional input operations.
- The method std::cin.ignore(n, delim) removes characters from the input stream until it reaches the delimiter delim, which can be up to n characters.
- It is frequently used to remove any faulty input that remains after a failed input procedure, following std::cin.clear.
- Display a prompt asking the user to enter a number.
- Read the input from the user using std::cin.
- Check if the input operation failed using std::cin.fail.
- If std::cin.fail returns true, display an error message and clear the error state of std::cin using std::cin.clear.
- Discard any remaining invalid input from std::cin using std::cin.ignore.
- Repeat steps 1-5 until a valid input is received.
- Once a valid input is received, process the input as needed.
std::cin.fail Method:
std::cin.clear Method:
std::cin.ignore Method:
Pseudocode:
Program 1:
Let us take an example to illustrate the std::cin.fail function in C++ .
Example
#include <iostream>
#include <limits> // Include the header for std::numeric_limits
int main() {
int nums;
std::cout << "Enter a number: ";
std::cin >> nums;
if (std::cin.fail()) {
std::cout << "Invalid input. Please enter a valid number." << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input
} else {
std::cout << "You entered: " << nums << std::endl;
}
return 0;
}
Output:
Program 2:
Let us take another example to illustrate the std::cin.fail function in C++.
Example
#include <iostream>
#include <limits> // for std::numeric_limits
int main() {
int nums2;
bool validIn= false;
while (!validIn) {
std::cout << "Enter a number: ";
std::cin >> nums2;
if (std::cin.fail()) {
std::cout << "Invalid input. Please enter a valid number." << std::endl;
std::cin.clear(); // Clear the error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input
} else {
validIn = true;
}
}
std::cout << "You entered: " << nums2<< std::endl;
return 0;
}
Output: