[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Help! what's wrong with this program

bbmmzz

10/6/2008 3:25:00 PM

Here is my program:
int main()
{
int ival;

while(cin >> ival, !cin.eof())
{
if(cin.bad())
return 0;
if(cin.fail())
{
cerr << "retry!" << endl;
cin.clear();
continue;
}
cout << ival << endl;
}
return 0;
}

Input int number, the program work well. But if input char, example
'a', it display "retry!" endless.
"cin >> ival" is skip after input 'a'? Why?
6 Answers

Obnoxious User

10/6/2008 3:51:00 PM

0

On Mon, 06 Oct 2008 08:25:06 -0700, bbmmzz wrote:

> Here is my program:
> int main()
> {
> int ival;
>
> while(cin >> ival, !cin.eof())
> {
> if(cin.bad())
> return 0;
> if(cin.fail())
> {
> cerr << "retry!" << endl;
> cin.clear();
> continue;
> }
> cout << ival << endl;
> }
> return 0;
> }
>
> Input int number, the program work well. But if input char, example 'a',
> it display "retry!" endless.
> "cin >> ival" is skip after input 'a'? Why?

Because the 'a' is still in the buffer of std::cin. You need to
remove it, otherwise it will try to read it again and again and
always fail.

See std::istream::ignore().

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_...

asm23

10/6/2008 4:11:00 PM

0

bbmmzz wrote:
> Here is my program:
> int main()
> {
> int ival;
>
> while(cin >> ival, !cin.eof())
> {
> if(cin.bad())
> return 0;
> if(cin.fail())
> {
> cerr << "retry!" << endl;
> cin.clear();
> continue;
> }
> cout << ival << endl;
> }
> return 0;
> }
>
> Input int number, the program work well. But if input char, example
> 'a', it display "retry!" endless.
> "cin >> ival" is skip after input 'a'? Why?

int main()
{
int ival;

while(cin >> ival&&!cin.eof()) //CHANGE HERE!
{
if(cin.bad())
return 0;
if(cin.fail())
{
cerr << "retry!" << endl;
cin.clear();
continue;
}
cout << ival << endl;
}
return 0;
}

This will work OK! But I can't explained it why...

bbmmzz

10/7/2008 2:20:00 AM

0

On 10?7?, ??12?11?, asm23 <asmwarr...@gmail.com> wrote:
> bbmmzz wrote:
> > Here is my program:
> > int main()
> > {
> > int ival;
>
> > while(cin >> ival, !cin.eof())
> > {
> > if(cin.bad())
> > return 0;
> > if(cin.fail())
> > {
> > cerr << "retry!" << endl;
> > cin.clear();
> > continue;
> > }
> > cout << ival << endl;
> > }
> > return 0;
> > }
>
> > Input int number, the program work well. But if input char, example
> > 'a', it display "retry!" endless.
> > "cin >> ival" is skip after input 'a'? Why?
>
> int main()
> {
> int ival;
>
> while(cin >> ival&&!cin.eof()) //CHANGE HERE!
> {
> if(cin.bad())
> return 0;
> if(cin.fail())
> {
> cerr << "retry!" << endl;
> cin.clear();
> continue;
> }
> cout << ival << endl;
> }
> return 0;
>
> }
>
> This will work OK! But I can't explained it why...- ??????? -
>
> - ??????? -

Thanks for your suggestion. Your change make the program check the
state of cin after "cin >> ival", and if fail it will exit while loop
and never let user retry. In my original program, the value of while
condition is dependon the expression "!cin.eof()".

I follow Obnoxious User's suggestion and add "cin.ignore()" after
"cin.clear()", it works.

James Kanze

10/7/2008 11:27:00 AM

0

On Oct 6, 5:25 pm, bbmmzz <bumanz...@gmail.com> wrote:
> Here is my program:
> int main()
> {
> int ival;

> while(cin >> ival, !cin.eof())

Could you please explain what this is supposed to do?
ios::eof() really doesn't have any usable meaning until you know
that an input has failed. It can return true even if the input
succeeds.

The standard idiom for a loop would be:

while ( std::cin >> ival ) ...

If you really want to put the error handling in the loop (which
I'm not sure is a good idea), you can do something like:

while ( std::cin >> ival || ! std::cin.eof() ) ...

(Note that in this case, you will only look at eof if the input
fails.)

> {
> if(cin.bad())
> return 0;
> if(cin.fail())
> {
> cerr << "retry!" << endl;
> cin.clear();
> continue;
> }
> cout << ival << endl;
> }
> return 0;
> }

> Input int number, the program work well. But if input char, example
> 'a', it display "retry!" endless.
> "cin >> ival" is skip after input 'a'? Why?

What did you do with the 'a'? I don't see where you extracted
it.

(Also, returning from the middle of a loop or using continue is
generally a sign of a poor program.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Hendrik Schober

10/7/2008 12:05:00 PM

0

James Kanze wrote:
> [...]
> (Also, returning from the middle of a loop or using continue is
> generally a sign of a poor program.)

While I can't remember having ever used 'continue', I
have no objections at all against returning from the
middle of a loop.

Schobi

Obnoxious User

10/7/2008 3:37:00 PM

0

On Tue, 07 Oct 2008 04:26:31 -0700, James Kanze wrote:

> (Also, returning from the middle of a loop or using continue is
> generally a sign of a poor program.)

Of course, those dreaded continues. Wasn't there a
letter called "Continue statement considered harmful" ;)

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_...