[comp.lang.c++] while

geiser@apollo.HP.COM (Wayne Geiser) (09/21/90)

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 <iostream.h>

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.

cline@cheetah.ece.clarkson.edu (Marshall Cline) (09/25/90)

In article <4cecce18.20b6d@apollo.HP.COM> geiser@apollo.HP.COM (Wayne Geiser) writes:
...
> #include <iostream.h>
> 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!
==============================================================================