473,378 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Help! what's wrong with this program

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?
Oct 6 '08 #1
6 1942
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_in_English
Oct 6 '08 #2
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...
Oct 6 '08 #3
On 10ÔÂ7ÈÕ, ÉÏÎç12ʱ11·Ö, asm23 <asmwarr...@gmail.comwrote:
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.
Oct 7 '08 #4
On Oct 6, 5:25 pm, bbmmzz <bumanz...@gmail.comwrote:
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:ja*********@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
Oct 7 '08 #5
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
Oct 7 '08 #6
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_in_English
Oct 7 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim21 | last post by:
OK, im miserable :)) so.. help'd b highly appreciated. Situation: Win XP aplication server RUNTIME (stored fmx files along with the ..hlp files) Fmx's generated and compiled on development...
0
by: Denise L. Moss-Fritch | last post by:
Has anyone developed context sensitive help for a C# application? According to our programming staff, the development side is not able to provide links without adding hard coded links (topic names)...
2
by: clintonG | last post by:
Is there a document that explains the difference between the Visual Studio .NET 2003 Combined Help Collection and the MSDN Library for Visual Studio .NET 2003 or does someone have comments...
3
by: Mark Lees | last post by:
Just installed 2003 and the help file sucks because it tries to connect to Microsoft Online Help which takes too long. When I run Access and I'm not connected to the internet, it seems to connect...
5
by: TD | last post by:
Hey All, I am hooking up our custom html (.chm) help file to our Access xp application, and, despite reading several posts and manuals on this, I still have a gap in my understanding... OK, so...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.