2
string go(string s, int& ind, node* cur,char &c)
{
    string f = "";
    if (cur->leftChild == NULL && cur->rightChild == NULL)
    {
        f = cur-> content;
    }
    else
    {
        if (s[ind] == 0x30)
        {
            ind++;
            go(s, ind, cur->leftChild,c);
        }
        else
        {
            ind++;
            go(s, ind, cur->rightChild,c);
        }

    }
    return f;// breakpoint here shows correct value 'e'
}
...
int main()
{
   string h = "";
   int ind = 0;
   string h = go(s, ind, &glob_root,c);
   cout << h << endl; // h is blank.
}

Turns out, that first time breakpoint on f hits, it shows value as 'e', what I want, but then it gets blank the following times it's hit.

if I change it to

string go(string s, int& ind, node* cur,char &c)
{
    if (cur->leftChild == NULL && cur->rightChild == NULL)
    {
       return cur-> content;
    }
    else
    {
        if (s[ind] == 0x30)
        {
            ind++;
            go(s, ind, cur->leftChild,c);
        }
        else
        {
            ind++;
            go(s, ind, cur->rightChild,c);
        }

    }

}

I get an access violation, since I have no return, If i add return ""; at the end, it just returns nothing, not 'e'

Any help appreciated

3
  • 14
    I don't know what your function is supposed to do, but you're ignoring return value of both recursive calls... Commented Jul 15, 2019 at 15:29
  • You should be writing something like f = go(...) (I guess). Commented Jul 15, 2019 at 15:31
  • it spans the tree according to directions in the string until it hits a leaf node, it shouldn't have output until that if(left == null && right == null) is true Commented Jul 15, 2019 at 15:32

1 Answer 1

2

If you hit the else branch in your first chunk of code, then nothing ever modifies f, so of course it stays empty and that empty string is what you end up returning.

You probably want to return go(... or at least capture the return value and do something with it that involves f or returning it directly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.