Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ltHank

macrumors newbie
Original poster
Nov 4, 2008
19
0
I'm having some trouble on a HW assignment. I'm not asking for a full solution (I'm not even telling you the assignment). What I do want to know is how the heck my output is changing! Please note the code below:

from main.cpp:
Code:
	number = A.getNumber("Mendoza", "Charles");
	number = A.getNumber("Chaplain", "Johnny");

Function a.
Code:
// getNumber
int Book::getNumber(string lname, string fname)
{
	int temp;
	
	temp = getNumber(lname, fname, root);
	
	cout << "outcheck" << endl;
	
	cout << temp << endl;
	
	return temp;
}

Function b.
Code:
// getNumber
int Book::getNumber(string lname, string fname, Card* card)
{
	
	if((lname == card->contact.getLast()) &&
	  (fname == card->contact.getFirst()))
	{
		int number = card->contact.getNumber();
		cout << number << "~" << endl;
		return number;
	}
	else
	{
		if(lname < card->contact.getLast())
		{
			getNumber(lname, fname, card->lchild);
		}
		else if(lname > card->contact.getLast())
		{
			getNumber(lname, fname, card->rchild);
		}
		else if(fname < card->contact.getFirst())
		{
			getNumber(lname, fname, card->lchild);
		}
		else if(fname < card->contact.getFirst())
		{
			getNumber(lname, fname, card->rchild);
		}
		else
		{
			cout << "ERROR: Name not found." << endl;
		}
	}
}

Output:
Code:
4831293~
outcheck
4831293
9984631~
outcheck
2

In the first case, you can see that the number 4831293 is returned just fine. However, for 9984631, it inexplicably changes to '2', everytime I run. I've tried a number of fixes, but none of them really fixed anything. Any idea what the heck is going wrong?
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
What happens if you try a third number? Do you get '2', or some other result? What happens if you swap the two main calls (address cards)? In other words, is there something about that particular number or contact card, or something about being the second number entered? Or something else?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I was typing something up about using the debugger, etc. but looked again and this one is pretty obvious. If getNumber calls itself recursively at all, it does nothing with the return value when it invokes itself. This means the return value of the initial invocation hits the closing } without a return statement. I would imagine your compiler would give you a warning about this, but maybe not. In any event, you need to return in all cases, that should take care of it.

-Lee
 

Ti_Poussin

macrumors regular
May 6, 2005
210
0
Indeed, the else condition into the int Book::getNumber(string lname, string fname, Card* card) function doesn't return a int, so you get a random number that was in the memory when the return value was initialize.

You should have indeed have receive a warning out of it, if not maybe you're compiling with g++ in command line, I would strongly recommend you to use
-Wall --pedantic flag, that really useful to get warning and error before and is good to make good code.

-Wall will show you all warning your code generate, warning are NOT normal, you shouldn't have any except in really special case.

--pedantic will turn warning into error to enforce good coding.

you may also want the -g flag to use gdb or some other debugger tools.

If you learn to code with those, you will learn to code a little better.
 

ltHank

macrumors newbie
Original poster
Nov 4, 2008
19
0
Ah, I see! That seems fairly obvious now... and yes, I did get a warning about it, but in my frustration I must have forgotten to actually read the darn thing! Phooey, I feel silly.

Thanks for the help, everyone!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.