1. ## 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);
}```

2. Do you have a question?

--
Mats

3. 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. Originally Posted by nicz888
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();```
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.

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

5. Originally Posted by matsp
Do you have a question?

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

6. sorry if it is asking for too much, can you show me where i need it fix it more in depth?

7. 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

8. #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' )
{