Thread: about recursion...

  1. #1
    Unregistered
    Guest

    about recursion...

    Code:
    void PlayerRemove(int &totalstones)
    /* remove a stone between 1-3 and check to make sure value of total is not 0 */
    {
    	int playerstones;
    	cin>>playerstones;
    
    	if ((playerstones>3)||(playerstones<1))
    	{
    		cout<<"Value must be between 1 and 3"<<endl;
    		cout<<"How many would you like? ";
    		PlayerRemove(totalstones);
    	}
    
    	if ((totalstones-playerstones)<=0)
    	{
    		cout<<"Value must be between 1 and "<<totalstones<<endl;
    		cout<<"How many would you like? ";
    		PlayerRemove(totalstones);
    	}
    	totalstones=totalstones-playerstones;//here is the problem i only want it to do the subtraction if the number is between 1-3
    }
    how does recursion work? does it finish the whole function or start a new one where it left off? and how should i change this so it wont subtract from total if the number is not between 1-3

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    11
    Eh, I don't think what you're trying to do should be done with recursion.

    Recursion calls the same function with a different value (HOPEFULLY THERE'S AN EXIT CONDITION SOMEWHERE), and will finish up the rest of the function after the recursive call after the 'other' functions have executed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 02:58 AM
  3. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  5. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM