Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8139379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:48:23+00:00 2026-06-06T11:48:23+00:00

First, let me apologize if this is the world’s stupidest question. But, I’m stumped

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:48:24+00:00Added an answer on June 6, 2026 at 11:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, let me apologize if this has been asked already, but I can’t
First of all, let me apologize if this question is too broad. I'm looking
Let me first apologize if this question could sound perhaps sort of amateurish for
First, let me apologize if this is a repeat question. I did a search
Aloha. This is my first question here after eons of lurking. I apologize for
Ok. First let me apologize profusely if this question has been covered. I did
First let me apologize for the scale of this problem but I'm really trying
First let me apologize a bit for the length of this post, it's mostly
First let me apologize if this doesn't make any sense. I took me some
Let me first apologize. I've been coding for a long time now, but I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.