In C++, fail() returns true when the last input or output operation did not work. The most common case is cin >> number when the user types letters instead of digits.
It returns true when failbit or badbit is set. Once a stream has failed, it ignores all later reads until you call clear(). Include <iostream> to use it.
This is a member function of std::ios. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.
Syntax
Call fail on a stream after reading from it. It takes no arguments.
C++
std::cin >> n;
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(1000, '\n');
}
Call
What it does
std::cin.fail()
Returns true if the last read from the keyboard failed.
file.fail()
Also true when a file could not be opened.
!std::cin
Same as fail(). The ! operator on a stream checks the failure flags.
Always check for failure after reading user input. A failed cin does not stop your program. It just silently does nothing.
Library declarations
C++
bool fail() const;
Parameters
Parameter
Description
None
fail() takes no parameters.
Return Value
Return Value
Description
true
failbit or badbit is set. The last operation failed.
false
The last operation worked. Note that eofbit alone does not make fail() true.
C++ Examples
Example 1 — Detect a Wrong Number with fail()
Read a number from text that has no digits and report the failure.
C++
#include <iostream>
#include <sstream>
int main() {
std::istringstream in("apple");
int n = 0;
in >> n;
if (in.fail()) {
std::cout << "Please type a number.\n";
} else {
std::cout << "You typed " << n << '\n';
}
return 0;
}
Output
Please type a number.
How It Works
The stream holds the word apple. An int cannot be built from it.
in >> n sets failbit and leaves n as 0.
in.fail() is true, so the if branch prints the friendly message.
Example 2 — Stop Summing at the First Bad Value
Add up numbers until a value cannot be read.
C++
#include <iostream>
#include <sstream>
int main() {
std::istringstream in("10 20 x 30");
int sum = 0;
int n = 0;
while (true) {
in >> n;
if (in.fail()) {
break;
}
sum += n;
}
std::cout << "sum = " << sum << '\n';
std::cout << "stopped at bad input\n";
return 0;
}
Output
sum = 30
stopped at bad input
How It Works
The loop reads one number per round. 10 and 20 are fine, so they are added to sum.
The third read meets the letter x. failbit is set and fail() becomes true, so break leaves the loop.
The 30 after the letter is never read. A failed stream does nothing until clear() is called.
Example 3 — fail() Is Not the Same as End of Input
Read the last value in the text. The stream reaches the end, but nothing failed.
The text has one number and then ends. in >> n reads 7 and must look one step further to see the number is over.
Looking past the last character sets eofbit, so eof() says yes.
The read itself worked, so failbit is not set and fail() says no. The next read would fail.
Frequently Asked Questions
It returns true when the last read from cin failed, for example when you typed letters but the program expected a number. It also returns true when badbit is set.
After a failed read, failbit stays set and the bad characters stay in the buffer. Every later read fails at once. Call cin.clear() and then cin.ignore() to fix it.
fail() means a read or write did not work. eof() means the end of the input was reached. Reading the very last value can set eof() without setting fail().
Yes. The ! operator on a stream returns true when fail() is true. So if (!cin) and if (cin.fail()) mean the same thing.
No. fail() only reports the state. To reset the state, call clear().