How to extract double from a stringstream

Viewed 2679

While working on my C++ project I've noticed something odd while playing with stringstream objects: I'm not able to extract a double from a stringstream using >>.

Consider this example:

#include <string>
#include <sstream>

int main(int argc, const char * argv[]) {

    std::string string("1000 ; 523277527397538 ; 0.183 ; 0.453 ; 0.5 ; 0.5 ; 0.033 ; 0 ; 0 ;");
    std::stringstream stringstream(string);
    int integer;
    char character;
    double doubleprec;
    stringstream >> integer >> character;
    stringstream >> integer >> character;
    stringstream >> doubleprec >> character;
    stringstream >> doubleprec >> character;

    return 0;
}

Using my debugger I've noticed that the variable integer takes first the value 1000 and then the value 523277527397538 (as I expected), but doubleprec takes always the value 0.

Why is that? Am I missing something about how streams work?

2 Answers
Related