Re: cin error recovery

From: Roman Gavrilov (clavrg_at_hotmail.com)
Date: 02/08/04


Date: Sat, 7 Feb 2004 21:45:41 -0800

My bad, full text would be:

int i1, i2;
cin >> i1;
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
cin >> i2;
cout << i1 << " " << i2 << endl;

so, I do clear the rest of the input buffer. Problem goes even deaper - does
not matter how many times after the error I try to read cin - it always
returns an error.

while (true) {
  int i1;
  cin >> i1;
  cout << i1;
}

if in the snippet above I enter error once, it will output the "weird" value
forever.

Regards,
Roman

"Jon Bell" <jtbellj3p@presby.edu> wrote in message
news:c047jb$svv$1@jtbell.presby.edu...
> In article <fe75986a.0402071558.a4cc4e0@posting.google.com>,
> Roman Gavrilov <clavrg@hotmail.com> wrote:
> >Have following snippet:
> >
> >int i1, i2;
> >cin >> i1;
> >cin >> i2;
> >cout << i1 << ", " << i2;
> >
> >If on first prompt I enter non-numeric value, then second value is not
> >prompted and i1, i2 are set to some weird value.
> >
> >Note: cin.clear() - does not help.
>
> Actually, cin.clear() *does* help, but it's not all you need to do.
>
> When stream input encounters an illegal character, input stops at that
> character, leaving it in the input stream so you can try to read it a
> different way if you want. In your case, you probably want to skip past
> it to the next hopefully valid input item.
>
> cin.clear() resets the stream status flags, which allows further input to
> take place, but it doesn't actually move the input point in the file. So
> if you try to read again, you just hit the bad data again.
>
> One common way to skip past the bad data is to assume that the items are
> separated by newlines, and use something like cin.ignore(1000, '\n') which
> skips to just past the next newline, or 1000 chars, whichever comes first.
> This works better if you prompt for the two numbers separately so they
> have to be on separate lines.
>
> If the two numbers are supposed to be on the same line, you could probably
> simply read the bad data into a dummy string using >>, which will read
> until the next whitespace; then try to read the next number normally.
>
> --
> Jon Bell <jtbellm4h@presby.edu> Presbyterian College
> Dept. of Physics and Computer Science Clinton, South Carolina USA



Relevant Pages

  • Re: is cin always the keyboards input?
    ... the whole point of 'cin' is to keep this detail hidden from you. ... > input stream) as cin. ... testdatamaker outputs strings every second, ...
    (comp.lang.cpp)
  • Re: std::cin.ignore() and std::cin.clear()
    ... So what does cleardo anyway, if not clear all cin data? ... It clears the error state of the stream. ... The extracts an unlimited amount of characters (the max is a sentinel ... C++ FAQ: http://www.parashift.com/c++-faq-lite/ ...
    (comp.lang.cpp)
  • Re: is cin always the keyboards input?
    ... the whole point of 'cin' is to keep this detail hidden from you. ... >> cat file | myapp ... Set stdin to non-blocking mode ... Here the stream will be in an error state. ...
    (comp.lang.cpp)
  • Re: Wierd newbie problem
    ... > You should flush cout before reading from cin. ... > it's possible for the text to not actually display ... > endl writes a newline and flushes the stream. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Wierd newbie problem
    ... You should flush cout before reading from cin. ... endl writes a newline and flushes the stream. ...
    (alt.comp.lang.learn.c-cpp)

Loading