Bad Returns! What is going on here?

Discussion in 'Mac Programming' started by ltHank, Nov 10, 2008.

  1. ltHank macrumors newbie

    Joined:
    Nov 4, 2008
    #1
    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?
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    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?
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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
     
  4. Ti_Poussin macrumors regular

    Ti_Poussin

    Joined:
    May 6, 2005
    #4
    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.
     
  5. ltHank thread starter macrumors newbie

    Joined:
    Nov 4, 2008
    #5
    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!
     

Share This Page