c++ - Return 不适用于 float 或 int,但适用于 string?

标签 c++ string function floating-point return

首先,如果这是世界上最愚蠢的问题,请允许我道歉。但是,我被难住了,我在这里和谷歌上进行了大量搜索。我正在自学 C++,因此我可能不需要掌握必要的词汇就知道要搜索什么。

我正在尝试编写一个有限状态机来解析方程。我知道以前有人做过,但我正在努力学习。为此,我希望能够获取字符串、识别数字并将它们转换为 double 或 float 。 (我会接受您对使用哪种格式的任何建议。)

我有一个将字符串转换为 double 的函数:

    double convertToDouble(string value)
{
    /* -- From http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
        Using stringstream, convert a string to a double by treating it like a stream
    */
    istringstream stream(value);
    double doubleValue;
    stream >> doubleValue;
    return doubleValue;
}

我有一个函数来查找字符串中的下一个数值:

string evaluateNextValue (int operatorPosition, string equation)
{
    /* -- Find the next value
        My idea is that, since I'm using spaces as my dividers, we'll look for
        the first number and then, using insert to put the individual numbers
        into a string until a space is found again. Then, the numbers--now
        in the correct order--can be converted to a double and returned
    */
    bool digitFound = false;
    string workingNumbers;
    for (int pos = operatorPosition; pos < equation.size(); pos ++)
    {
        if (equation.at(pos) == ' ' && digitFound == true)
        {
            double result = convertToDouble(workingNumbers);
            cout << "Converting a string to " << result << endl;
            cout << "The result plus one is: " << result +1 << endl;
            return workingNumbers;
        } else if (equation.at(pos) == ' ' && digitFound == false)
        {
            cout << "Skipping a blank space." << endl;
            continue;
        } else
        {
            if (digitFound == false)
            {
                digitFound = true;
                cout << "First digit found." << endl;
            }
            cout << "Adding " << equation.at(pos) << " to the string." << endl;
            workingNumbers.insert(workingNumbers.end(),equation.at(pos));
        }
    }
}

这是我用来调用它们的 main() 作为一种测试。

int main()
{
    string dataInput;
    cout << "Insert a number" << endl;
    getline(cin, dataInput);
    cout << "You entered: " << dataInput << endl;
    double numberValue = convertToDouble(evaluateNextValue(0, dataInput));

    cout << "Adding ten: " << numberValue + 10;
    return 0;
}

事情是这样的:就像现在一样,evaluateNextValue() 返回一个字符串,它可以工作。对我来说这似乎有点笨拙(也许这对你来说看起来很笨拙),但它确实有效。

当我让代码在函数中操作变量结果时,它工作得很好。我只需将字符串转换为 double 型即可使用它。

但是,当我将字符串转换为 double 型并尝试返回 double 型时。 。 。 double 在函数本身中工作得很好。但到达 main() 时却是 nan。甚至更奇怪(或者至少同样奇怪)的是尝试返回 int 确实返回 int,但从来没有任何远程连接到我输入的值。

如果您愿意提供任何帮助,我将不胜感激。而且,由于这是我在这里发表的第一篇文章,我愿意接受任何风格指针。

最佳答案

如果由于 for 循环条件导致 evaluateNextValue 到达字符串末尾,则返回值未定义(因为您没有 return那里有声明)。这会触发未定义的行为,其中可能包括返回 NaN 值。

您应该启用编译器的警告来捕获此类错误。

关于c++ - Return 不适用于 float 或 int,但适用于 string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11244413/

相关文章:

c++ - 如何模板化部分模板特化?

string - 确定变量是字符串还是数字字符串的 FoxPro 函数

ios - 如何在函数参数中指定协议(protocol)?

c - 确定函数的 Big-O 表示法

c++ - 在 R 中使用外部共享库

c++ - 使空间与 C++ 输入流完美配合

javascript - 如何通过 JavaScript 中的正则表达式将 camelCase 更改为 slug-case(或 kebab-case)

Java数组分割字符串

c - 我有一个函数,它有 int a 和 int b 我所要做的就是找到最大值

c# - Windows 8 中是否有任何特殊的 API 来挂载 ISO 文件?