string parsing to double fails in C++

别等时光非礼了梦想. 提交于 2019-12-24 09:12:41

问题


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?


回答1:


Perhaps this is the problem you're having: 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.




回答2:


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



来源:https://stackoverflow.com/questions/2634580/string-parsing-to-double-fails-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!