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
}
Call
What 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
Parameter
Description
None
good() takes no parameters.
Return Value
Return Value
Description
true
The stream is healthy: eofbit, failbit, and badbit are all clear.
false
At 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;
}
Output
a = 42, b = 7
good after first read: yes
good after second read: no
How It Works
std::istringstream acts like cin, but it reads from the text "42 7" instead of the keyboard.
After reading 42 there is still text left, so no flag is set and good() is true.
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.
A new stream with text inside starts in the good state, so the first line says good.
in >> n cannot turn the letters abc into an int. The read fails and the stream sets failbit.
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;
}
Output
after bad read, good: no
after clear, good: yes
word: abc
How It Works
The failed read leaves the letters abc unread in the stream and sets failbit.
clear() sets the state back to goodbit, so good() returns true again.
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.