tags:

views:

101

answers:

1
stringstream parser;
 short top = 0;
         parser << 5;
         parser >> top;
             parser.str("");//HERE I'M RESETTING parser
         parser << 6; //DOESN'T PUT 6 INTO parser
         short bottom = 0;
         parser >> bottom;

Why doesn't it work?

+6  A: 

Typically to 'reset' a stringstream you need to both reset the underlying sequence to and empty string with str and to clear any fail and eof flags with clear.

parser.str( std::string() );
parser.clear();

Typically what happens is the first >> reaches the end of the string and sets the eof bit, although it successfully parses the first short. Operations on the stream after this immediately fail because the stream's eof bit is still set.

Charles Bailey
@Charles yes, it sets eof after operator>>() has been used and sometimes eof can set fail as well. Thanks
There is nothing we can do
Personally I think `parser.str("")` is clearer, but that's a matter of opinion.
T.E.D.