Reputation: 586
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
Upvotes: 2
Views: 381
Reputation: 31468
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.
Upvotes: 2