while (cin >> d) does not work if d = int, double, float

Peter Schmid schmid@snake.iap.physik.tu-darmstadt.de
Sun Nov 26 16:55:00 GMT 2000


Reading numbers from cin or from a file in a loop does not work, since eof is
not processed correctly.
Running the following program on my i686-pc-linux-gnu box (GLIBC 2.1.3
gcc-20001120) 

cat tr.C
#include <iostream>

int main()
{
    int i;
    while (std::cin >> i)
        std::cout << i << '\n';
}

-- 
Generates the following output, when I type e.g. 12 and the press ^D.
peter@kiste:~/> g++ tr.C
peter@kiste:~/>./a.out 
12 (^D is given)
12
12
12
12
12
12
12
12
12
12
12
repeated until the program is terminated by a signal.
It does not matter if the variable is an int, a float, a short, or a double;
reading a string or a char works though. Reading former types from a file
does not work with a while loop of the kind while (file >> i) either.

I believe this should be fixed. (I'm writing to this list since the original
prs-mailing list seems to be dead). 

Peter Schmid



More information about the Libstdc++ mailing list