Thread: Restaurant

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Restaurant

    The problem I'm having is in my getData(int choice) function. I want the function to keep asking the user to enter a valid number (1-8) if they enter something else. If I put an if...else loop there, I can only prevent them from entering the wrong number once.


    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    struct menuItemType
     {
    	string menuItem;
    	double menuPrice;	
    	int numOrdered;
    };
    	menuItemType theMenu[7];
    
    void showMenu(); // display the menu
    int getData(int choice); // get input from the user
    double Round(double &totalTax); // round to the nearest cent
    void printCheck(double &tax, double &totalBill); // print what the person ordered and their total check
    
    
    int main()
    {	
    	int choice; //	The number the user chooses for the item they want	
    	int number; // Used in the getData function
    	int x; // used in "for" loop to initialize numOrdered to 0.
    	double bill; //	This is the total for items purchased, without tax.  
    	double totalBill; //  This is the total for items purchased, with tax.
    	double tax; // Amount of one tax after it is rounded.  
    	double taxRate; // Percentage of tax to be charged. 
    	double totalTax; //	Total amount of tax.
    	char shouldContinue; //	Used to loop the do...while function
    	
    	totalBill = 0;
    	taxRate = .05;
    	tax = 0;
    	totalTax = 0;
    	bill = 0;
    	number = 0;
    	choice = 0;
    	x = 0;
    
    	theMenu[0].menuItem  = "#1  Plain Egg";	// This part sets the menuItem array  to a numbered food list showing prices.
    	theMenu[1].menuItem  = "#2  Bacon and Egg";
    	theMenu[2].menuItem  = "#3  Muffin";
    	theMenu[3].menuItem  = "#4  French Toast";
    	theMenu[4].menuItem  = "#5  Fruit Basket";
    	theMenu[5].menuItem  = "#6  Cereal";
    	theMenu[6].menuItem  = "#7  Coffee";
    	theMenu[7].menuItem  = "#8  Tea";
    
    	theMenu[0].menuPrice  = 1.45;	// This sets the menuPrice array to the price of the food. 
    	theMenu[1].menuPrice  = 2.45;
    	theMenu[2].menuPrice  = 0.99;
    	theMenu[3].menuPrice  = 1.99;
    	theMenu[4].menuPrice  = 2.49;
    	theMenu[5].menuPrice  = 0.69;
    	theMenu[6].menuPrice  = 0.50;
    	theMenu[7].menuPrice  = 0.75;
    
    	for (x = 0; x < 8; x++) // Initialize theMenu[x].numOrdered to all 0's.  
    	{
    		theMenu[x].numOrdered = 0;
    	}
    	
    	do
    	{
    	showMenu();  // run the void showMenu() function
    	cout << "\n" << endl;
    
    	choice = getData(number); // Returns the number the person chose the the array number
    
    	bill = theMenu[choice].menuPrice + bill; // Keeps a running total of the bill
    	
    	theMenu[choice].numOrdered++; //Add 1 to the amount of the item ordered.  
    
    	cout << "\n" << theMenu[choice].numOrdered << setw (20) << theMenu[choice].menuItem << " \n" << endl;
    
    	cout << "Would you like to place another order?  (Y/N)" << endl; // Allow to purchase more
    	cin >> shouldContinue;
    	}
    	while (shouldContinue == 'Y' || shouldContinue == 'y');
    	cout << endl;
    
    	totalTax = bill * taxRate;
    
    	tax = Round(totalTax);
    	
    	totalBill = bill + tax;
    
    	printCheck(tax, totalBill);
    
    	return 0;
    } //main () 
    
    
    
    //***********//
    // showMenu()//
    //**********//
    
    void showMenu()
    {
    	int x;
    	for (x = 0; x <=7; x++)
    	cout << theMenu[x].menuItem << "\t" << theMenu[x].menuPrice << endl; 
    }
    
    
    //*****************//
    // getData(int number)//
    //****************//
    int getData(int number)
    {
    	int z;
    	z = 0;
    	//****** What I'd like to do here is make sure the user chooses a number 1-8.  
            // I'm not sure how to make a loop that will keep asking them to re-enter a number until they choose a valid choice.*******//
    	cout << "Please choose a food item by the corresponding number." << endl;
    	cin >> z; 
            z = z - 1; // Subtract 1 from the foodChoice to make it match the array.			
    
    	return z;	
    }
    
    //*********************//
    // Round(double &totalTax)//
    //*******************//
    double Round(double &totalTax) //Round to the nearest cent.  
       {
       double (tax);
       
       tax = totalTax * 100.0 + 0.5;
       return tax / 100.0;
       }
    
    //**************************************//
    // void printCheck(double &tax, double &totalBill)//
    //************************************//
    void printCheck(double &tax, double &totalBill)
    {
    	int x;
    	x = 0;
    
    	cout << "Welcome to Johnny's Restaurant \n" << endl;
    	for (x = 0; x < 8; x++)
    	{
    	if (theMenu[x].numOrdered > 0)
    	{
    	cout << theMenu[x].numOrdered << "\t" << theMenu[x].menuItem << "\t" << right << theMenu[x].menuPrice << right << endl; 
    	}
    	else 
    	{
    		cout << "";
    	}
    	}
    
    	cout << "Tax" << setw (27) << tax << endl;
    	cout << "Amount Due" << setw (20) << totalBill << endl;
    }
    Last edited by Salem; 02-16-2011 at 01:39 AM. Reason: It's [code] tags, not any other kind!!!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use the same kind of while loop you managed to use in main()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    enum

    In general for this I would write an enumerated list to label the menuItems, then you can use this in a switch statement inside of the suggested while(..) loop, if none of the switch cases are met, ie the user has entered an invalid option, then the default case in the switch can catch this and output the error, otherwise exit the while loop and return the selection.

    Code:
    enum itemCode {
    
    	PL_EGG,
    	BC_EGG,
    	MUFFIN,
    	FR_TOAST,
    	//etc
    };
    
    //..
    //...
    
    // in your get() function...
    
    bool good2go = false;
    
    while(!good2go)
    {
    	//user inputs selection
    	//...
    	
    	 switch(selection)
    	 {
    		case PL_EGG:
    		{
    			//set the valid value, whatever
    			good2go = true;
    		}
    		break;
    
    		//more cases
    
    		default:
    		//output invalid choice message
    		break;
    	 }	
    }
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    i try that run my program and it end at this progam only.

    cout << "Would you like to place another order? (Y/N)" << endl; // Allow to purchase more
    cin >> shouldContinue;
    }
    while (shouldContinue == 'Y' || shouldContinue == 'y');
    cout << endl;
    why it not continued in the end??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Restaurant Point-of-Sale Application: Help?!
    By matt.s in forum C Programming
    Replies: 14
    Last Post: 04-16-2010, 05:36 PM
  2. Replies: 2
    Last Post: 12-07-2009, 04:45 AM
  3. structures fwrite
    By dlcp in forum C Programming
    Replies: 1
    Last Post: 04-30-2008, 03:01 PM
  4. movie being filmed in my town, some actors went to my restauratn
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-15-2003, 01:51 PM
  5. Restaurant Drive thrus
    By Scourfish in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-07-2001, 12:58 AM