简体   繁体   中英

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. I'm teaching myself C++, so it's possible I just don't have to vocabulary necessary to know what to search for.

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.

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. 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(). 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.

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). This triggers undefined behaviour, which can include returning NaN values.

You should enable your compiler's warnings to catch such errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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