I can get the code to compile, and essentially do what I want it to. 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. Any help is greatly appreciated.

#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.  
   {
   int 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;
}

Recommended Answers

All 5 Replies

if.. else is not a loop. Try using the do..while(condiition);,while(condition), or for(init;condition;increment) loops. e.g.

choice = -1;
while(choice < 0 || choice >= sizeof(theMenu)){
   showMenu();  // run the void showMenu() function
   cout << "\n" << endl;
   choice = getData(number); // Returns the number the person chose the the array number
   }
choice = getData(number);

while (choice < 0 || choice > 7)
{
    cout << "Invalid entry." << endl;
    showMenu();
    choice = getData(number);
}

This will simply loop until the user enters a number that is not less than 1 and not greater than 8.

I probably shouldn't write the code for you like that but you seem to be trying and have an understanding of how to get it done.

A couple pointers:
1) Don't use global variables like this: menuItemType theMenu[7]; 2) You're overdoing the comments. I know you've been told to use them but don't use them where the code makes it obvious what is happening. For example:

void showMenu(); // display the menu
int choice; //	The number the user chooses for the item they want

Most of your comments would apply as well. If your teacher is making you do this, maybe it's just to get you in a habit but it really makes the code more difficult to read.
3) Initialize variables when you declare them. Do this:

double bill = 0.0;

Instead of this:

double bill;
...
bill = 0;

Along the same lines, declare your loop variables in the loop:

for (int x = 0; x < 8; x++)
...

Instead of:

int x;
...
x = 0;
...
for (x = 0; x < 8; x++)

4) If a parameter to your functions is passed by-reference and you don't change it in your function, make it a const:

double Round(const double &totalTax)
...

In the case that you haven't learned about const, just keep it in mind for the future or perhaps ask your teacher about it.

I know you didn't ask for a critique but these are good things to work on so that you use good practice early on.

if.. else is not a loop. Try using the do..while(condiition);,while(condition), or for(init;condition;increment) loops. e.g.

choice = -1;
while(choice < 0 || choice >= sizeof(theMenu)){
   showMenu();  // run the void showMenu() function
   cout << "\n" << endl;
   choice = getData(number); // Returns the number the person chose the the array number
   }

I think that should be:

choice < 0 || choice > sizeof(theMenu)/sizeof(menuItemType)

But I don't think that's a good option.

One more thing:

theMenu has only 7 elements: menuItemType theMenu[7]; But you access 8 elements. 0-7.

If you want 8 elements it should be: menuItemType theMenu[8];

Thank you for the help with the code and the critique. I appreciate any advice I can get on here. There is only so much that can be covered in the classroom and I know it's easier to start good habits than break bad ones.

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.