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

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, clear() sets the error state of a stream. With no argument it resets the state to goodbit, which removes failbit, eofbit, and badbit.

Despite its name, it does not erase any data and it does not empty the input buffer. It only fixes the flags. 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 clear on a stream. You can pass a state value, but most programs call it with no arguments.

C++
std::cin.clear();                    // back to goodbit
std::cin.clear(std::ios::eofbit);    // set exactly this state
CallWhat it does
std::cin.clear()Resets all flags to goodbit. cin accepts reads again.
s.clear(std::ios::eofbit)Replaces the state with only eofbit. It does not add to the old flags.
std::cin.ignore(1000, '\n')Usually written right after clear() to remove the bad characters.

The standard fix for bad input is a pair: cin.clear() and then cin.ignore(). Use them in that order.

Library declarations
C++
void clear(iostate state = goodbit);

Parameters

ParameterDescription
stateOptional. The new state value. The default is std::ios::goodbit, which means no flags. Any value you pass replaces the old state.

Return Value

Return ValueDescription
Nothingclear() returns void. The state of the stream is changed.

C++ Examples

Example 1 — Read Again After a Failure

Fail on a word, clear the flags, skip the word, and read the number behind it.

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

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

    in >> n;
    std::cout << "first read failed: " << (in.fail() ? "yes" : "no") << '\n';

    in.clear();
    std::string skip;
    in >> skip;

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

How It Works

  1. in >> n fails on the letters abc. They stay in the stream and failbit is set.
  2. clear() removes the flag. Then in >> skip reads abc into a string and gets it out of the way.
  3. The next in >> n finds 5 and works. Without clear() this last read would fail without even trying.

Example 2 — clear() with a State Argument

Passing a flag replaces the state. This is handy for tests.

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

int main() {
    std::stringstream s;

    s.clear(std::ios::failbit);
    std::cout << "fail: " << (s.fail() ? "yes" : "no") << '\n';
    std::cout << "good: " << (s.good() ? "yes" : "no") << '\n';

    s.clear();
    std::cout << "fail after clear(): " << (s.fail() ? "yes" : "no") << '\n';
    std::cout << "good after clear(): " << (s.good() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. clear(std::ios::failbit) sets the state to exactly failbit. The name clear is a little misleading here.
  2. With failbit set, fail() is true and good() is false.
  3. clear() with the default argument sets goodbit, so the stream is healthy again.

Example 3 — Go Back to the Start After End of Input

Clear eofbit and use seekg() to read the same text again.

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

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

    in >> a;
    std::cout << "eof: " << (in.eof() ? "yes" : "no") << '\n';

    in.clear();
    in.seekg(0);
    in >> b;

    std::cout << "a = " << a << ", b = " << b << '\n';
    return 0;
}

How It Works

  1. Reading 8 reaches the end of the text, so eofbit is set.
  2. clear() removes the end flag and seekg(0) moves the read position back to the first character.
  3. The second read sees 8 again, so both a and b are 8.

Frequently Asked Questions

It resets the error flags of cin, so cin works again after a failure. It does not remove characters from the buffer.
The wrong characters are still waiting in the input buffer. If you do not remove them with ignore(), the next read sees them and fails again.
No. The name is confusing. It clears only the state flags. The data and the read position stay the same.
A state such as std::ios::eofbit, failbit, or badbit, or several joined with |. The old flags are replaced, not added to.
clear() repairs the stream state. ignore() removes characters from the input. After bad input you normally need both, clear() first.

Next: close()

Flush and disconnect a file stream from its file.

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

8 people found this page helpful