C++ std::ios::fail() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

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');
}
CallWhat 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::cinSame 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

ParameterDescription
Nonefail() takes no parameters.

Return Value

Return ValueDescription
truefailbit or badbit is set. The last operation failed.
falseThe 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;
}

How It Works

  1. The stream holds the word apple. An int cannot be built from it.
  2. in >> n sets failbit and leaves n as 0.
  3. 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;
}

How It Works

  1. The loop reads one number per round. 10 and 20 are fine, so they are added to sum.
  2. The third read meets the letter x. failbit is set and fail() becomes true, so break leaves the loop.
  3. 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.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("7");
    int n = 0;

    in >> n;

    std::cout << "n = " << n << '\n';
    std::cout << "fail: " << (in.fail() ? "yes" : "no") << '\n';
    std::cout << "eof:  " << (in.eof() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. The text has one number and then ends. in >> n reads 7 and must look one step further to see the number is over.
  2. Looking past the last character sets eofbit, so eof() says yes.
  3. 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().

Next: flush()

Send buffered output to its destination now.

flush() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful