Dima
Dima

Reputation: 172

Resetting cin stream state C++

Here Im trying to get an integer from user, looping while the input is correct.

After entering non integer value (e.g "dsdfgsdg") cin.fail() returns true, as expected and while loop body starts executing.

Here I reset error flags of cin, using cin.clear(); and cin.fail() returns false, as expected.

But next call to cin doesn't work and sets error flags back on.

Any ideas?

#include<iostream>
using namespace std;

int main() {
  int a;
  cin >> a;

  while (cin.fail()) {
    cout << "Incorrect data. Enter new integer:\n";
    cin.clear();
    cin >> a;
  }
} 

Upvotes: 2

Views: 986

Answers (4)

smac89
smac89

Reputation: 43078

After cin.clear(), you do this:

#include <iostream> //std::streamsize, std::cin
#include <limits> //std::numeric_limits
....
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')

What the above does is that it discards any characters that are still left in the stream until EOF or until it has discarded the delimiter (\n in this case).

Without doing this, cin >> a; will continue reading the same non-integer characters and your program will keep looping.

Upvotes: 6

Phoned_Leek25
Phoned_Leek25

Reputation: 72

There is also a very easy method:

#include <iostream>
std::cin.clear() //reset stream state so that cin.fail() becomes false.
std::cin.ignore(INT_MAX,'\n'); //clear the stream from any remaining characters until the \n which is present at the end of any cin.

This will reset stream state and clear any remaining things in it. Much easier, less code, and less header files.

Upvotes: 1

Pandrei
Pandrei

Reputation: 4951

cin.clear() does not clear the buffer; it resets the error flags. So you will still have the sting you entered in your buffer and the code will not allow you to enter new data until you clear the cin buffer. cin.Ignore() should do the trick

Upvotes: 1

Zac Howland
Zac Howland

Reputation: 15872

As a matter of style, prefer this method:

int main() 
{
    int a;
    while (!(std::cin >> a))
    {
        std::cout << "Incorrect data. Enter new integer:" << std::endl;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
}

Upvotes: 2

Related Questions