简体   繁体   English

Return对float或int不起作用,但是对string有效吗?

[英]Return doesn't work with float or int, but does work with string?

First, let me apologize if this is the world's stupidest question. 首先,让我道歉,如果这是世界上最愚蠢的问题。 But, I'm stumped and I've done a bunch of searching both here and on Google. 但是,我很困惑,在这里和Google上都做了很多搜索。 I'm teaching myself C++, so it's possible I just don't have to vocabulary necessary to know what to search for. 我正在自学C ++,所以有可能我不必掌握必要的词汇来知道要搜索的内容。

I'm trying to write a Finite State Machine to parse equations. 我正在尝试编写有限状态机来解析方程式。 I know it's been done before, but I'm trying to learn. 我知道以前已经做过,但是我正在尝试学习。 To that end, I want to be able to take a string, recognize numbers, and convert them to doubles or floats. 为此,我希望能够使用字符串,识别数字并将其转换为双精度或浮点型。 (I'll entertain any advice you have on which format to use.) (如果您对使用哪种格式有任何建议,我都会接受。)

I have a function to convert a string to a 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;
}

And I have a function to look for the next numeric value in a string: 我有一个函数来查找字符串中的下一个数字值:

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

And this is the main() I'm using to call them both as a sort of test. 这是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;
}

Here's the thing: as it is now, with the evaluateNextValue() returning a string, it works. 事情就是这样:到现在为止,evaluateNextValue()返回一个字符串,它可以工作。 It seems a bit ungainly to me (may it all seems ungainly to you), but it works. 对我来说似乎有点笨拙(也许对您来说一切似乎都很笨拙),但它确实有效。

When I have the code manipulate the variable result in the function, it works fine. 当我让代码操纵函数中的变量结果时,它可以正常工作。 I just convert the string to a double and I can work with it. 我只是将字符串转换为双精度字,我就可以使用它。

BUT , when I convert the string to a double and try to return the double. 但是 ,当我将字符串转换为双精度型并尝试返回双精度型时。 . . the double works fine in the function itself. 双重功能本身很好。 But it's nan when it arrives in main(). 但是当它到达main()时是难事。 Even weirder (or just as weird, at any rate) is the fact that trying to return an int DOES return an int, but never anything remotely connected to the value I enter. 甚至更奇怪(或无论如何也很奇怪)的事实是,尝试返回一个int确实会返回一个int,但绝不会有任何远程连接到我输入的值。

I'd appreciate any help you care to offer. 感谢您提供的任何帮助。 And, as this is my first post here, I'm open to any style pointers. 并且,因为这是我在这里的第一篇文章,所以我对任何样式指针都开放。

The return value is undefined if evaluateNextValue arrives at the end of the string due to the for loop condition (because you have no return statement there). 如果由于for循环的条件, evaluateNextValue到达字符串的末尾,则返回值是不确定的(因为那里没有return语句)。 This triggers undefined behaviour, which can include returning NaN values. 这将触发未定义的行为,其中可能包括返回的NaN值。

You should enable your compiler's warnings to catch such errors. 您应该启用编译器的警告以捕获此类错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM