The C++ code below does int
to string
and a string
to int
conversions. Then, it repeats these steps again. The stringst
After reading i3
from the stream, the eof bit of the stream gets set. This is because reading an integer from the stream makes the stream read until either it reads a nondigit character or it runs out of characters to read. When the latter that happens, the eof bit is set.
For example, I can change this line,
stream1.str(s2);
to this,
stream1.str(s2 + " ");
so when stream1 >> i3;
is executed, it will encounter a nondigit character (' '), stop reading and not set the eof bit.
You have to unset the eof bit by .clear
or some other method before attempting to read from it.