字符串解析在C ++中双重失败 [英] string parsing to double fails in C++

查看:173
本文介绍了字符串解析在C ++中双重失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有趣的,我一直在努力弄清楚。我有以下程序:

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();
}

输出为:

0
1

知道为什么这是失败。请告诉我我在做什么?

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

感谢,

helixed

编辑:

好的,很抱歉将其变成 double post ,但这看起来像一个特定于Xcode的问题。如果我编译这个在g ++,代码工作没有问题。有没有人知道为什么这是发生在Xcode,我怎么可能解决它?

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?

推荐答案

也许这是问题您拥有: http://stackoverflow.com/questions/1377038/stringstream-question

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

请参阅接受的答案其中的链接。 Apple讨论链接中的示例非常类似于您遇到的问题。

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

这篇关于字符串解析在C ++中双重失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆