Thread: Mutiple switches

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    Mutiple switches

    I am trying to work out a switch for a program for ordering pizzas. The user should first have a choice of of they would like to start an order or print grand totals. If they choose to print grand totals the program prints the grand totals, if they choose to start an order then the program gets input about the order and then ask if they would like to add another pizza to this order or end the order. Choosing to add another pizza gets input for the next pizza and chosing to end the order prints the information for that order. Then after that order prints the program should ask if the user wants to start another order or print the grand totals. etc, etc... This is what I have so far, the functions that I call are listed below main:

    Code:
    int main ()
    {
    
    AskOrder ();
    
    do
    	{
    
    
    		do
    		{
    			cout<<"get input\n";
    				
    		}while (AskPizza () != 2); 
    
    		do
    		{
    			cout<<"print order\n";
    			
    		}while (AskPizza () != 1);
    		
    	
    	}while (AskOrder () != 2);
    
    
    
    do
    	{
    		cout<<"print grand\n";
    		
    	}while (AskOrder () != 1);
    
    
    
    
    	return 0; 
    }
    
    // functions 
    
    int AskPizza ()
    {
    
    	int choice;
    
    	cout<<"Please Choose One:\n";
    	cout<<"1. Add a pizza to this order\n";
    	cout<<"2. End this order\n";
    	cout<<"Selection : ";
    	cin>>choice;
    
    	switch (choice)
    	{
    	case 1 : return 1; break;
    	case 2: return 2; break;
    
    	}
    
    }
    
    int AskOrder ()
    {
    
    	int choice;
    
    	cout<<"Please Choose One:\n";
    	cout<<"1. Enter an Order\n";
    	cout<<"2. Get Grand Totals\n";
    	cout<<"Selection : ";
    	cin>>choice;
    
    	switch (choice)
    	{
    	case 1 : return 1; break;
    	case 2 : return 2; break;
    	}
    
    }
    The way I have the grand total just won't print at all, even if I choose option two. No matter what option I choose in AskOrder the program asks for the input of a pizza. If I input a pizza and then say I want to end the order the program does print the order but then it asks for another pizza when it should be asking me if I want another order or if I want the grand total. Any suggestion would help thank you!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In your first call to AskOrder you ignore the return value.

    Perhaps you want a while loop instead of a do-while loop there, so you can check the return value of AskOrder first, before entering the loop.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    By the way, looking at the following:
    Code:
    	switch (choice)
    	{
    	case 1 : return 1; break;
    	case 2: return 2; break;
    
    	}
    ...didn't it occur to you just to return choice? As it is the function returns garbage if the user input something other than 1 or 2 (this garbage, by the way, can be 1 or 2). You probably should use a loop and ask for "Dammit, I said, choose 1 or 2" until this condition is met.

    (You won't need the break statement either, because program flow will never reach it.)

    Added:
    Or, you might return 0 to indicate that user did not input 1 or 2.
    Code:
        if (choice == 1 || choice == 2) return choice;
        else return 0;
    Last edited by anon; 04-26-2007 at 03:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple command line arguments with switches
    By Ene Dene in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2009, 03:28 AM
  2. simple question regarding switches
    By ove256 in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-24-2005, 12:01 AM
  3. Looping in Switches
    By Kaminaga in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2005, 07:59 PM
  4. switches and breaks
    By volk in forum C Programming
    Replies: 11
    Last Post: 01-10-2003, 08:55 PM
  5. Microswitch joysticks and arcade switches
    By Scourfish in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-30-2001, 07:50 PM