Re: cin error recovery
From: Roman Gavrilov (clavrg_at_hotmail.com)
Date: 02/08/04
- Next message: Jonathan Turkanis: "Re: cin error recovery"
- Previous message: Les Cargill: "Re: Announcing new scripting/prototyping language"
- In reply to: Jon Bell: "Re: cin error recovery"
- Next in thread: Jonathan Turkanis: "Re: cin error recovery"
- Reply: Jonathan Turkanis: "Re: cin error recovery"
- Reply: E. Robert Tisdale: "Re: cin error recovery"
- Reply: David Harmon: "Re: cin error recovery"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Jonathan Turkanis: "Re: cin error recovery"
- Previous message: Les Cargill: "Re: Announcing new scripting/prototyping language"
- In reply to: Jon Bell: "Re: cin error recovery"
- Next in thread: Jonathan Turkanis: "Re: cin error recovery"
- Reply: Jonathan Turkanis: "Re: cin error recovery"
- Reply: E. Robert Tisdale: "Re: cin error recovery"
- Reply: David Harmon: "Re: cin error recovery"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|