Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!rpi!clarkson!news.clarkson.edu!cline From: cline@cheetah.ece.clarkson.edu (Marshall Cline) Newsgroups: comp.lang.c++ Subject: Re: while (cin >> ch) ... when does it stop? Message-ID: Date: 25 Sep 90 04:22:39 GMT References: <4cecce18.20b6d@apollo.HP.COM> Sender: news@news.clarkson.edu Reply-To: cline@sun.soe.clarkson.edu (Marshall Cline) Organization: (I don't speak for the) ECE Dept, Clarkson Univ, Potsdam, NY Lines: 53 In-Reply-To: geiser@apollo.HP.COM's message of 20 Sep 90 17:44:00 GMT Nntp-Posting-Host: cheetah.ece.clarkson.edu In article <4cecce18.20b6d@apollo.HP.COM> geiser@apollo.HP.COM (Wayne Geiser) writes: ... > #include > main() { > char ch; > int ct = 0; > while (cin >> ch) > ct++; > cout << ct << " characters\n"; > return 0; > } ... > The man page for IOS (this is from the AT&T version 2.1 documentation > set) on page 3 says: > "Two operators are defined to allow convenient checking of the error > state of an ios: operator!() and operator void*(). The latter The `operator void*()' is the one you're getting in the above loop. The while loop wants a truth value, which means either an `int' which can be compared against `0', or a pointer that can be compared against the zero pointer. Since one (and only one) of these two choices is available, it chooses to use `operator void*()'. > 2.1) and it works are Lippman describes (returns false on eof). I have > also tried this on Turbo C++ and it loops forever. Borland's > documentation reads very much like the AT&T man page. I documented this bug in TC++'s library in my review for The C++ Report (the issue was this last July, I believe). Unfortunately due to copy limitations (as well as ambiguity in the AT&T specs), it had to be dropped. I still have it recorded in my TC++ bug list (anonymous ftp from sun.soe.clarkson.edu [128.153.12.3] in ~ftp/pub/Turbo-C++/bug-list). The ambiguity with the AT&T specs involves whether it is `bad()', or `fail()' or some other mechanism that ought to be triggered on `eof()'. Regardless, everyone (including Borland) seems to agree that the idiom you reproduced above ought to work; it's just the exact mechanics of getting it to work that are dubious. > Thoughts? > BTW, I know to use cin.get(ch) to make this work in both environments. I'm sure you're aware that operator>> skips whitespace but get(ch) doesn't. Marshall Cline -- ============================================================================== Marshall Cline / Asst.Prof / ECE Dept / Clarkson Univ / Potsdam, NY 13676 cline@sun.soe.clarkson.edu / Bitnet:BH0W@CLUTX / uunet!clutx.clarkson.edu!bh0w Voice: 315-268-3868 / Secretary: 315-268-6511 / FAX: 315-268-7600 Career search in progress; ECE faculty; research oriented; will send vita. PS: If your company is interested in on-site C++/OOD training, drop me a line! ==============================================================================