Thread: while loop question

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

    while loop question

    Code:
    #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()
    {	
    	
    	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);

    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.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't actually see a question in your post, what was your question?

    Some unsolicited comments on the code...

    Don't use recursion to do the same thing over:
    Code:
    			if ( (lowVal == 0 && maxVal == 0))
    				printNum();
    You will potentially overflow the stack if you do that for long enough, and it's not really what you want anyways, I bet.

    Why not use a switch:
    Code:
        			        if (input == int('s') || input == int('S'))
    				{
                                             ...
    				}
    				else if (input == int('c') || input == int('C'))
    				{
                                             ...
    				}
    				else if (input == int('f') || input == int ('F'))
    				{
                                             ...
    				}
    				else if (input == int('t') || input == int ('T'))
    				{
                                             ...
    				}
    				else if (input == int('x') || input == int ('X'))
    				{
                                             ...
    				}
    Instead use:
    Code:
    			        switch(input) 
                                        case 's':
                                        case 'S':
                                           ...
                                           break;
    
                                        case 'c':
                                        case 'C':
                                           ...
                                           break;
                                             ...
    				}
    Also, you don't need "int('s')" etc - just 's' will do.

    --
    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
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, if you type 's' then the first condition will be false. The second condition, however, will be true, so you'll enter the loop. May-be you'll need &&?

    However, typically a menu like that is done with a switch:

    Code:
    char input;
    do {
         get_input;
         input = tolower(input);
         switch (input) {
            case 's':
                handle_this_option;
                break;
            case '...'
             ...
            default:
                cout<< "Incorrect input\n";
        }
    } while (input != 'q');
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    thanks
    Last edited by nicz888; 11-28-2007 at 08:15 AM. Reason: never mind

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 05:13 PM