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

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, good() tells you whether a stream is in a perfectly healthy state. It returns true only when none of the three problem flags is set: eofbit (end of input), failbit (a read or write failed), and badbit (a serious stream error).

Every stream keeps these flags inside itself. good() is the strictest check of all: even reaching the end of the input makes it return false. 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 good on any stream, such as std::cin, std::cout, or a file stream. It takes no arguments.

C++
bool ok = in.good();

if (std::cin.good()) {
    // safe to keep reading
}
CallWhat it does
in.good()Returns true when no flag at all is set.
std::cin.good()Checks the keyboard input stream the same way.
if (in)A shorter test. It is true when fail() is false, so it ignores the end-of-file flag.

good() is stricter than if (stream). After the last value is read, if (in) may still be true, while in.good() is already false.

Library declarations
C++
bool good() const;

Parameters

ParameterDescription
Nonegood() takes no parameters.

Return Value

Return ValueDescription
trueThe stream is healthy: eofbit, failbit, and badbit are all clear.
falseAt least one of those three flags is set.

C++ Examples

Example 1 — good() After Each Read

Read two numbers from a string stream and check good() after each one.

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

int main() {
    std::istringstream in("42 7");
    int a = 0;
    int b = 0;

    in >> a;
    bool afterFirst = in.good();
    in >> b;
    bool afterSecond = in.good();

    std::cout << "a = " << a << ", b = " << b << '\n';
    std::cout << "good after first read: " << (afterFirst ? "yes" : "no") << '\n';
    std::cout << "good after second read: " << (afterSecond ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. std::istringstream acts like cin, but it reads from the text "42 7" instead of the keyboard.
  2. After reading 42 there is still text left, so no flag is set and good() is true.
  3. Reading 7 reaches the end of the text, so eofbit is set. Both numbers were read correctly, yet good() is already false.

Example 2 — good() After Bad Input

Try to read a number from letters. The read fails, so the stream is no longer good.

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

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

    std::cout << "before: " << (in.good() ? "good" : "not good") << '\n';
    in >> n;
    std::cout << "after:  " << (in.good() ? "good" : "not good") << '\n';
    std::cout << "fail(): " << (in.fail() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. A new stream with text inside starts in the good state, so the first line says good.
  2. in >> n cannot turn the letters abc into an int. The read fails and the stream sets failbit.
  3. Because one flag is set, good() is false and fail() is true. n stays 0.

Example 3 — Make a Stream Good Again with clear()

Reset the flags, then read the word that caused the failure.

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

int main() {
    std::istringstream in("abc 9");
    int n = 0;
    std::string word;

    in >> n;
    std::cout << "after bad read, good: " << (in.good() ? "yes" : "no") << '\n';

    in.clear();
    std::cout << "after clear, good: " << (in.good() ? "yes" : "no") << '\n';

    in >> word;
    std::cout << "word: " << word << '\n';
    return 0;
}

How It Works

  1. The failed read leaves the letters abc unread in the stream and sets failbit.
  2. clear() sets the state back to goodbit, so good() returns true again.
  3. Now the stream accepts reads again. Reading into a std::string works, so word becomes abc.

Frequently Asked Questions

It returns true when none of eofbit, failbit, or badbit is set on the stream. If any of them is set, it returns false.
No. if (stream) is true when fail() is false, so it is still true when only eofbit is set. good() is also false after the end of input is reached.
Reading the last value can reach the end of the input, which sets eofbit. The value was read correctly, but the stream is no longer in the perfectly good state.
No. Use while (cin >> x) instead. It reads first and then tests the result, so you never process a failed read.
Call clear(). It sets the state flags back to goodbit. If the bad input is still waiting in the buffer, remove it with ignore() before reading again.

Next: ignore()

Skip and throw away characters from the input.

ignore() 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.

7 people found this page helpful