views:

154

answers:

2

Here's a fun one I've been trying to figure out. I have the following program:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(int argc, char *argv[])
{
    string s("5");
    istringstream stream(s);

    double theValue;
    stream >> theValue;

    cout << theValue << endl;
    cout << stream.fail();
}

The output is:

0
1

I don't understand why this is failing. Could somebody please tell me what I'm doing wrong?

Thanks,

helixed

EDIT:

Okay, sorry to turn this into a double post, but this looks like a problem specific to Xcode. If I compile this in g++, the code works without a problem. Does anybody have an idea why this is happening in Xcode, and how I could possibly fix it?

Thanks again,

helixed

+3  A: 

Are you sure that's exactly what you're building? I get 5 and 0 as expected

Michael Mrozek
Okay, so this works fine in g++. I edited my post above. Thanks for the quick reply.
helixed
+2  A: 

Perhaps this is the problem you're having: http://stackoverflow.com/questions/1377038/stringstream-question

See the accepted answer and the link therein. An example in the Apple discussion link sounds very much like what you're experiencing.

Fred Larson
So that was the problem. It fixed the output for both of my posts. It's amazing Apple would let a bug like this slip through. Thanks for the help.
helixed