Thread: Recursion Function Problem

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Recursion Function Problem

    Code:
    #include <cstdlib> 
    #include <ctime>
    #include <iomanip>
    #include <iostream>
    
    using namespace std; // prototype and name space declaration
    int sqr(int);
    int cube(int);
    int fourth(int);
    int fifth(int);
    
    bool printNum();
    void show(int (*fn)(int), int); 
    
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    
    void main()
    {	
    	
    	while(printNum())
    		;
    
    }
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    
    bool printNum()
    {
    	char input;
    	int block =0;
    	
    	cout<< "To Square Enter:           S"<<endl;
    	cout<< "To Cube Enter:             C"<<endl;
    	cout<< "To the Fourth Power Enter: F"<<endl;
    	cout<< "To the Fifth Power Enter:  H"<<endl;
    	cout<< "To Quit: Q"<<endl;
    	cin >> input;
    	
    	while ( input != 's' && input != 'S'
    			 && input != 'c' && input != 'C'
    			  && input != 'f' && input != 'F'
    			   && input != 'h' && input != 'H'
    			    && input != 'q' && input != 'Q')
    	{		
    
    		cout << "Wrong Choice.\n";
    		cout << "Enter Again: ";
    		cin.clear();
    		cin.ignore(80,'\n');
    		cin >> input;
    	}
    	
              if ( input == 'q' || input =='Q')
    			{
    			cout << "Program Ended. "<<endl;
    			return 0;
    				
    				}
    	while (true)
    
    		{
    
    			int lowVal ,maxVal;
    
    
    			cout <<"Enter A Number For Low Bound: ";
    			cin >> lowVal;
    			
    			cout <<"Enter A Number For Low Bound: ";
    			cin >> Val;
    		
    			if ( input == 'q' || input =='Q')
    				{
    				cout << "Program Ended. "<<endl;
    				return 0;
    				
    				}
    			if ( (lowVal == 0 && maxVal == 0))
    				printNum();
    
    			cout <<"\n";
    			cout <<"Printing "<<(maxVal-lowVal)+1<<" numbers"<<endl;
    			cout <<"\n";
    
    			for (int x = lowVal; lowVal<=maxVal; lowVal++)
    			{
    				switch (input)
    				{
    				case 's':
    					{
    					cout.setf(ios::left);
    					cout<< setw(5);
    					show(sqr, lowVal);
    					block++;
    					break;
    					}
    
    				case 'S':
    					{
    					cout.setf(ios::left);
    					cout<< setw(5);
    					show(sqr, lowVal);
    					block++;
    					break;
    					}
    				case 'c':
    					{
    					cout.setf(ios::left);
    					cout<< setw(5);
    					show(cube, lowVal);
    					block++;
    					break;
    					}
    
    				case 'C':
    					{
    					cout.setf(ios::left);
    					cout<< setw(5);
    					show(cube, lowVal);
    					block++;
    					break;
    					}
    								
    				case 'f':
    					{
    					cout.setf(ios::left);
    					cout<< setw(8);
    					show(fourth, lowVal);
    					block++;
    					break;
    					}
    
    				case 'F':
    					{
    					cout.setf(ios::left);
    					cout<< setw(8);
    					show(fourth, lowVal);
    					block++;
    					break;
    					}
    				case 'h':
    					{
    					cout.setf(ios::left);
    					cout<< setw(8);
    					show(fifth, lowVal);
    					block++;
    					break;
    					}
    
    				case 'H':
    					{
    					cout.setf(ios::left);
    					cout<< setw(8);
    					show(fifth, lowVal);
    					block++;
    					break;
    					}
    
    				}
    
    				if (block == 10)
    				{
    					block = 0;
    					cout<<"\n";
    				}
    			}
    			cout <<"\n";
    			block = 0;
    			
    	}
    
       
    }
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    //------------------------------------------------------------------
    int sqr(int num) // list of functions 
    {
        return num * num;
    }
    
    int cube(int num) // this one is to cube
    {
        return num * num * num;
    }
    int fourth(int num)
    {
        return num * num * num * num;
    }
    int fifth(int num)
    {
        return num * num * num * num * num;
    }
    
    void show(int (*fn)(int), int num) // pointer
    {
        cout << fn(num);
    }
    Last edited by nicz888; 11-28-2007 at 09:28 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you have a question?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    here is what i want it to do
    1)choose a function and then enter two number for interval boundary.
    2)unless user enter two 0, it will keep asking for two "new number" to as the interval and display all number in between with respect power.
    3)when user enter 0 and 0 the original option will appear and ask user to choice a new function or quit

    here is my problem
    1)when i enter q the first time the prompt is up, the program will end
    2)however when i enter q after i had go through a function already the program will not end,
    3)how do i fix that

    here is what i input and the program output
    1)
    choice a function
    i enter q the program ends.
    2)
    choice a function
    i enter s for square
    i enter 2 and 4 for boundary
    it prints
    4 and 8
    i enter 0 and 0 for boudary
    it ask me to enter another function or quit
    i enter q
    my out put is a prompt asking me to enter the number for the boundary.

    i want it to end when i enter q. is this clear???

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nicz888 View Post
    here is what i want it to do
    1)choose a function and then enter two number for interval boundary.
    2)unless user enter two 0, it will keep asking for two "new number" to as the interval and display all number in between with respect power.
    3)when user enter 0 and 0 the original option will appear and ask user to choice a new function or quit

    here is my problem
    1)when i enter q the first time the prompt is up, the program will end
    2)however when i enter q after i had go through a function already the program will not end,
    Like I said yesterday, don't use recursion to run the code again when a loop will do. This is what happens:
    Code:
    		if ( (lowVal == 0 && maxVal == 0))
    				printNum();
    So your callstakc is:
    Code:
    main
      printNum
         printNum
    You exit out of the inner printNum, and then you are in the outer printNum function - you should use a loop construct.
    3)how do i fix that
    Consider the above and the solution will be relatively easy
    here is what i input and the program output
    1)
    choice a function
    i enter q the program ends.
    2)
    choice a function
    i enter s for square
    i enter 2 and 4 for boundary
    it prints
    4 and 8
    i enter 0 and 0 for boudary
    it ask me to enter another function or quit
    i enter q
    my out put is a prompt asking me to enter the number for the boundary.

    i want it to end when i enter q. is this clear???
    Clear as mud.

    Some other comments:
    Code:
    				case 's':
    					{
    					cout.setf(ios::left);
    					cout<< setw(5);
    					show(sqr, lowVal);
    					block++;
    					break;
    					}
    
    				case 'S':
    					{
    					cout.setf(ios::left);
    					cout<< setw(5);
    					show(sqr, lowVal);
    					block++;
    					break;
    					}
    Is the same as:
    Code:
    				case 'S':
    				case 's':
    					{
    					cout.setf(ios::left);
    					cout<< setw(5);
    					show(sqr, lowVal);
    					block++;
    					break;
    					}
    You have "block++" several times in different case statements. You really only need one block++ outside the switch statement.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Quote Originally Posted by matsp View Post
    Do you have a question?

    --
    Mats
    sorry i didn't post the question quicker, it took me a while to type it.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    sorry if it is asking for too much, can you show me where i need it fix it more in depth?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Which part of my answers don't you understand? You are using recursion to ask the "square, cube, etc" question, when you should be using a(nother) while-loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1.
    Code:
    void main()
    The main function returns an int, not void.


    #2.
    Code:
    int lowVal ,maxVal;
    
    cout <<"Enter A Number For Low Bound: ";
    cin >> lowVal;
    			
    cout <<"Enter A Number For Low Bound: ";
    cin >> Val;
    Typos?

    #3. Your use of recursion here, for this particular problem, is ill advised. It gains you nothing and is inappropriate to what you're trying to accomplish. Your problems are because of the recursive calls being made. In particular, pay attention to this:
    Code:
    cout <<"Enter A Number For Low Bound: ";
    cin >> lowVal;
    			
    cout <<"Enter A Number For Low Bound: ";
    cin >> Val;
    
    if ( input == 'q' || input =='Q')
    {
        cout << "Program Ended. "<<endl;
        return 0;
    }
    if ( (lowVal == 0 && maxVal == 0))
       printNum();
    If you enter 0 0 for the bounds, you recursively call the printNum function which gets you your menu printed asking to choose a function. When you enter 'Q', you for some reason choose to have the user reenter the bounds again when they shouldn't have to (they already said they wanted to quit). The recursive call to printNum then ends after a second use of 0 0 for the bounds which only takes you back to the first call made to printNum whereupon you try to go through the subsequent for-loop/switch and then eventually back to the beginning of the while(true) loop. You really need to rearrange things here.

    I'd suggest something more along the lines of:
    Code:
    char choice = menu();  // Call a menu function to get users choice
    while( choice != 'q' && choice != 'Q' )
    {
        // Ask for the bounds.
    
        if( both bounds != 0 )
        {
            // Put your for-loop/switch code here
        }
    
        choice = menu();  // Call menu function again to get user's choice
    }
    Last edited by hk_mp5kpdw; 11-28-2007 at 09:51 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  5. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM