Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!apollo!apollo.hp.com!geiser From: geiser@apollo.HP.COM (Wayne Geiser) Newsgroups: comp.lang.c++ Subject: while (cin >> ch) ... when does it stop? Message-ID: <4cecce18.20b6d@apollo.HP.COM> Date: 20 Sep 90 17:44:00 GMT Sender: root@apollo.HP.COM Reply-To: geiser@apollo.HP.COM (Wayne Geiser) Organization: Hewlett-Packard Apollo Division - Chelmsford, MA Lines: 67 This is a "Should I do as they say or do as they do?" question. On page 93 of "The C++ Primer," Stanley Lippman presents a program. For brevity, I've reduced it to the one below: #include main() { char ch; int ct = 0; while (cin >> ch) ct++; cout << ct << " characters\n"; return 0; } He intends (and explains) that this should read characters from the standard input device until eof (among other things) and then the condition in the while statement should fail. In fact, on pages 383-384 he says: "A more general method of reading from the input stream is to make the extraction operation the truth condition of a while loop. For example, char ch; while ( cin >> ch ) // ... reads a character at a time from standard input. When end-of-file is encountered, the truth condition evaluates as false and the loop terminates." 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 converts an ios to a pointer so that it can be compared to zero. The conversion will return 0 if failbit or badbit is set in an error state, and will return a pointer value otherwise." Note that it will not be set on end-of-file. I have tried this program on both the AT&T cfront (versions 2.0 and 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. Thoughts? BTW, I know to use cin.get(ch) to make this work in both environments. Wayne. Wayne Geiser Apollo Computer, Inc. - A subsidiary of Hewlett Packard geiser@apollo.hp.com "Nolan Ryan is pitching much better now that he has his curve ball straightened out." - Joe Garagiola.