#include <cstdlib> 
#include <ctime>
#include <iomanip>
#include <iostream>

using namespace std; // prototype and name space declaration
int sqr(int);
int cube(int);
int fourthPower(int);
int fifthPower(int);
int sixthPower(int);
bool printNum();
void show(int (*fn)(int), int); 

//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

void main()
{	
	
		
	char first[30], last[30];  

    cout << "Please Enter Your First Name: ";
    cin >> first;
			
    cout << "\n";
	cout << "Please Enter Your Last Name: ";
    cin >> last;

    cout << "\n\nThank you " << first << " " << last;
	cout << "\n";
	cout << "First choose a power and then "
		 << "Enter the Bound limits        "<<endl;

	cout <<"Upper Bound And Lower Bound "
		 <<"need to be range of 0 and 100"<<endl;
	
	cout << "Begins Excerise";

	cout << "\n\n";
	printNum();

}
//------------------------------------------------------------------
//------------------------------------------------------------------
//------------------------------------------------------------------

bool printNum()
{
	char input;
	int block =0;

	cout<< "To Square The Number Enter: S"<<endl;
	cout<< "To Cube The Number Enter: C"<<endl;
	cout<< "To the Fourth Power of The Number Enter: F"<<endl;
	cout<< "To the Fifth Power of The Number Enter: T"<<endl;
	cout<< "To the Sixth Power of The Number Enter: X"<<endl;
	cout<< "To Quit: Q"<<endl;
	cin >> input;
	
	while ( input!= int('s') || input !=int('S')
		|| input != int('c') || input !=int('C')
		|| input != int('f') || input !=int('F')
		|| input != int('t') || input !=int('T')
		|| input != int('x') || input !=int('X')
		|| input != int('q') || input !=int('Q'))
	{		
			if ( input == int('q') || input ==int('Q'))
		{
		cout << "Program Ended. "<<endl;
			return 0;
		}
		cout << "Wrong Choice.\n";
		cout << "Enter Again: ";
		cin.clear();
		cin.ignore(80,'\n');
		cin >> input;
	}

			
	while (true)

		{

			int lowVal ,maxVal;


			cout <<"Enter A Number For Low Bound: ";
			cin >> lowVal;
					
			while ( !cin.good() ||( lowVal < 0 || lowVal >100 )) // validation  
			{

				cout << "Positive Numbers Only.\n";
				cout << "Enter Again: ";
				cin.clear();
				cin.ignore(80,'\n');
				cin >> lowVal;
			}
			cout << "Enter A Number For Upper Bound : ";
			cin >> maxVal;

			while ( !cin.good() || maxVal < lowVal || maxVal >100 ) 
			{
				cout << "Positive Numbers Only.\n";
				cout << "Upper Bound Can' Be Smaller Than Lower Bound";
				cout << "Enter Again: ";
				cin.clear();
				cin.ignore(80,'\n');
				cin >> maxVal;
			}
		
			if ( (lowVal == 0 && maxVal == 0))
				printNum();

			cout <<"Printing "<<(maxVal-lowVal)+1<<" numbers"<<endl;

			for (int x = lowVal; lowVal<=maxVal; lowVal++)
			{
				if (input == int('s') || input == int('S'))
				{
					cout.setf(ios::left);
					cout<< setw(5);
					show(sqr, lowVal);
					block++;
				}
				else if (input == int('c') || input == int('C'))
				{
					cout.setf(ios::right);
					cout<< setw(5);
					show(cube, lowVal);
					block++;
				}
				else if (input == int('f') || input == int ('F'))
				{
					cout.setf(ios::right);
					cout<< setw(8);
					show(fourthPower, lowVal);
					block++;
				}
				else if (input == int('t') || input == int ('T'))
				{
					cout.setf(ios::right);
					cout<< setw(10);
					show(fifthPower, lowVal);
					block++;
				}
				else if (input == int('x') || input == int ('X'))
				{
					cout.setf(ios::right);
					cout<< setw(10);
					show(sixthPower, lowVal);
					block++;
				}
				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 fourthPower(int num)
{
    return num * num * num * num;
}
int fifthPower(int num)
{
    return num * num * num * num * num;
}

int sixthPower(int num)
{
    return num * num * num * num * num * num;
}

void show(int (*fn)(int), int num) // pointer
{
    cout << fn(num);
}

first of all i want to thank you all for help me on the other thread.
after reading all the comments, i realized that i am shouldn't be posting a different question
on a same thread so i am posting this question on a new thread.

1)i am offering user 6 options, s,S,c,C...q and Q
if user inputs s for square and then the program will prompt user to input 2 number one for upper and one for lower bound.
afterward i will display all the number in between the boundary and square all the numbers
and prints them.
after the print it will ask the user again for a new sets of boundary and if the user inputs 2 0's. my program will bring up the original prompt and start fresh again.

2)the loop i set i want it to catch the user input if they enter anything other than s,S,c,C..q and Q. i will display a error message and ask for input again.

3)i entered this
input output
2 wrong input
a wrong input
s wrong input
q quit

4)when i enter q the i am surprise program ends, but when i enter s or any of the characters in the option i offer my loop will keep re promote for another input why.


thanks

Recommended Answers

All 9 Replies

3)i entered this
input output
2 wrong input
a wrong input
s wrong input
q quit

Did you try my suggestion? Output various values at key spots in the program to see what values are not as expected.

4)when i enter q the i am surprise program ends, but when i enter s or any of the characters in the option i offer my loop will keep re promote for another input why.

I'm still wondering why you are surprised the program ends. Isn't that what Q and q mean? You did something right, it seems.

will, when i enter s or S my loop should said ok you picked function square, now move on and chose two numbers.

but what really happen here is when i enter s or S, my loop said sorry wrong choice enter again.
and my program wasn't able to perform the square function that i want it to do.

what suprise me is that when i enter q or Q my loop understand that the user wanted to quit,

i don't understand why is my while loop at line 63-69 is not working properly.

is that clear???

no need to cast all the literal char value to an int.

Evaluate each section of the conditional starting on line 63 individually. All options in the conditional must be true to continue the loop. If input == any of the desired char then the loop should stop. Therefore I believe pseudocode for the conditional starting on like 63 should be:

while input != 'q' and input != 'Q' and input != 's' and etc,  
  display error message 
  input another value.

There is no need to clear the input stream for invalid char input.

Once valid char input has been achieved then you can go through the serial statements to do whatever you want for each individual selection.

Did you try my suggestion? Output various values at key spots in the program to see what values are not as expected.

will, when i enter s or S my loop should said ok you picked function square, now move on and chose two numbers.

but what really happen here is when i enter s or S, my loop said sorry wrong choice enter again.
and my program wasn't able to perform the square function that i want it to do.

For the third time DID YOU TRY MY SUGGESTION? OUTPUT YOUR VARIABLES!!!

ok i fixed the loop part. now can you help me with this question.

i am posting my new code here, because i dont have the option to edit my original 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);
}

again
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???

Look carefully at the flow of your code

Display menu
accept input
While bad input
{
    accept input
}  // end of while

if (quit)
{
    quit
}  // end of if

loop forever
{
    do stuff
    never enter [B]input[/B]
    never break out of loop
}  // end of forever loop
done.

That's what I see. You never are asked to enter another function.

When you added outputs to see what variables had what values, what did they tell you?

Make the while loops nested. You can figure out which one goes inside the other and how much the outer loop should encompass.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.