From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,HEADER_SPAM, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b30bd69fa8f63cb2 X-Google-Attributes: gid103376,public X-Google-Thread: fc772,b30bd69fa8f63cb2 X-Google-Attributes: gidfc772,public X-Google-ArrivalTime: 2003-06-18 08:45:01 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!uwm.edu!rpi!not-for-mail From: John Potter Newsgroups: comp.lang.ada,comp.lang.c++.moderated Subject: Re: C bug of the day Date: 18 Jun 2003 11:47:12 -0400 Organization: EarthLink Inc. -- http://www.EarthLink.net Sender: cppmods@netlab.cs.rpi.edu Message-ID: <3p5vevg1frvldbivj001au91flboebatgr@4ax.com> References: Reply-To: jpotter@penguin.lhup.edu NNTP-Posting-Host: netlab.cs.rpi.edu X-Original-Date: Tue, 17 Jun 2003 22:33:44 GMT X-Submission-Address: c++-submit@netlab.cs.rpi.edu X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated iQBVAwUAPvCJfEHMCo9UcraBAQF9VwH+PAn+AayiXwmz4GTosXFJqbMhfQo3/kSw 2fMZfhO0ExXj0qKmZz79LTHeVeU7GPCH/rAfHHhb5NF0EN3z4a/imw== =RhTn Xref: archiver1.google.com comp.lang.ada:39401 comp.lang.c++.moderated:68633 Date: 2003-06-18T11:47:12-04:00 List-Id: On 17 Jun 2003 13:51:19 -0400, kanze@gabi-soft.fr wrote: > The second problem is with: > while ( cin.get() != '\n' ) ... > if end of file or any error occurs, you have an endless loop. As an > unrepentant Pascal programmer, I would write: > while ( std::cin.peek() != EOF && std::cin.peek() != '\n') ... ;-) > More typical C++ idioms might be: > char ch ; > while ( cin.get( ch ) && ch != '\n' ) ... > or: > int ch ; > while ( (ch = cin.get()) != EOF && ch != '\n' ) ... > or, if you don't like uninitialized variables (as I don't): > for ( int ch = cin.get() ; > ch != EOF && ch != '\n' ; > ch = cin.get() ) ... Nice. Or cin.ignore(INT_MAX, '\n'); [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]