Help! what's wrong with this program

B

bbmmzz

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?
 
A

asm23

bbmmzz said:
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...
 
B

bbmmzz

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.
 
J

James Kanze

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.)
 
H

Hendrik Schober

James said:
[...]
(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top