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 8743849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:45:01+00:00 2026-06-13T11:45:01+00:00

I’m trying to write a simple function to extract the next number from an

  • 0

I’m trying to write a simple function to extract the next number from an expression in a string and my problem is that the reference parameter ioIsValidExpression is not being updated after the function exits. It looks like I am actually referencing the correct variable (and not a temporary) since the addresses of test3 (as it is called before calling getNextNumber(…)) and ioIsValidExpression (its name when inside the function) are the same.

I expect this to be an embarassing mistake somewhere but I’m getting nowhere trying to figure this one out; I’ve included my int main and int getNextNumber with a comment line where I return a value from the function.

int getNextNumber(std::string& iExpression, int& ioLoopShift, bool& ioIsValidExpression)
{
    //spaces are ignored, a minus is a unary minus (so must be seated next to a number (no spaces between '-' and number))
    int length = iExpression.size(); //evaluate only once
    bool unaryMinusFound = false;
    bool numberFound = false;

    int oSubExpression = 0;
    for(int i =0; i < length; i++)
    {
        if((int)iExpression[i] == 32)
        {
            if(!numberFound)
            {
                if(!unaryMinusFound)
                {
                    continue;
                }
                else
                {
                    ioIsValidExpression = false;
                    td::cout << "address of ioIsValidExpression: " << &ioIsValidExpression << ", value: " << ioIsValidExpression << "\n";
                    return 0;
                }
            }
            else
            {
                return unaryMinusFound? -1 * oSubExpression: oSubExpression;
            }
        }
        if((int)iExpression[i] == 45 && !unaryMinusFound)
        {
            unaryMinusFound = true;
            continue;
        }
        if((int)iExpression[i] >= 48 && (int)iExpression[i] <= 57)
        {
            numberFound = true;
            oSubExpression = (int)(iExpression[i]-48) + 10*oSubExpression;
            ioLoopShift++;
            continue;
        }
        else
        {
            //garbage characters
            ioIsValidExpression = false;
            return 0;
        }
    }
    return unaryMinusFound? -1 * oSubExpression: oSubExpression;
}

int main:

int main(int argc, char* argv[])
{
    std::string test = " - 45 & ";
    int test2 = 0;
    bool test3 = true;
    std::cout << getNextNumber(test,test2,test3) << ", address of test3: " << &test3 << ", value: " << test3 << "\n";

    return 0;
}

output from running the program:

address of ioIsValidExpression: 0xbf86e58f, value: 0
0, address of test3: 0xbf86e58f, value: 1
  • 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-13T11:45:02+00:00Added an answer on June 13, 2026 at 11:45 am

    The order in which function arguments (and your output statement is a chain of functions) are evaluated is unspecified. Consider how it works correctly upon holding the result of the function in a variable before outputting the whole thing: test

    int temp = getNextNumber(test, test2, test3);
    std::cout << temp << ", address of test3: " << &test3 << ", value: " << test3 << "\n";
    

    Output:

    address of ioIsValidExpression: 0xbfaa85cf, value: 0
    0, address of test3: 0xbfaa85cf, value: 0

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.