I have an assignment where I'm supposed to make a restaurant menu. The menu must loop until the user enters 4 to get the bill. I'm supposed to use a function to display the menu, and another function to calculate the running total AND display the bill when the user finishes ordering.
Problem is, I have to initialize runningTotal before I use it, and I don't know where to put it to stop it resetting to its initialization. Originally I had my calculating function, cost(), set up like this:
void cost(const double ITEM_PRICE, int amtOfItemOrdered, bool isDoneOrdering) {
// initializing runningTotal and declaring other variables...
double runningTotal = 0, tip, finalBill;
// if statement so bill only displays when order is finished...
if (isDoneOrdering == false)
// calculate running total...
runningTotal += (ITEM_PRICE * amtOfItemOrdered);
else {
// when order finishes, calculate and display bill...
tip = (runningTotal * 0.20);
finalBill = (runningTotal + tip);
cout << "\nFood total: " << runningTotal <<
"\nTip: " << tip <<
"\nFinal Bill: " << finalBill;
}
Which I think results in runningTotal resetting to 0 every time the function executes. When I run the program, order something, and press 4 to see the bill, everything in the bill comes up as 0. Here's an example of the output:
Food total: 0
Tip: 0
Final Bill: 0
I thought it might help to move runningTotal outside the cost() function like this:
int main() {
// initializing runningTotal with 0 in main...
double runningTotal = 0;
// other variables...
const double ITEM1_PRICE = 2.50, ITEM2_PRICE = 3.50, ITEM3_PRICE = 4.50;
int itemOrdered, amtOfItemOrdered;
bool isDoneOrdering = false;
// loop menu choice until order is finished...
do {
displayMenu(ITEM1_PRICE, ITEM2_PRICE, ITEM3_PRICE);
cout << "Enter your choice: ";
cin >> itemOrdered;
// don't ask "how many" when done ordering...
if (itemOrdered != 4) {
cout << "\nHow many? ";
cin >> amtOfItemOrdered;
}
switch (itemOrdered) {
case 1:
// passing runningTotal to cost() so cost() can use it...
cost(ITEM1_PRICE, amtOfItemOrdered, isDoneOrdering, runningTotal);
break;
case 2:
cost(ITEM2_PRICE, amtOfItemOrdered, isDoneOrdering, runningTotal);
break;
case 3:
cost(ITEM3_PRICE, amtOfItemOrdered, isDoneOrdering, runningTotal);
break;
case 4:
isDoneOrdering = true;
cost(0, 0, isDoneOrdering, runningTotal);
// zeroes are here because cost() asks for parameters,
// but we're done adding to running total...
break;
default:
0; // empty placeholder statement...
}
} while (isDoneOrdering == false);
}
But this has the same result. I think what's happening here is that runningTotal in main is different from runningTotal in cost(), and the value 0 in main's runningTotal is getting passed to the runningTotal in cost(), which is resetting it anyway?
I thought about making a return statement for runningTotal, tip, and finalBill, and displaying the bill outside of the function, but I think the assignment wants me to stick to void functions.
Just tested initializing runningTotal to 1 in main, and the bill displays the following:
Food total: 1
Tip: 0.2
Final Bill: 1.2
So I do think initialization is the problem here. Any help would be appreciated. Thanks.
Since this is C++, it makes sense to use classes.
Consider a class to encapsulate the cost calculation, and a separate class for storing the menu.
Instantiate both classes in
main. Having one function that focuses on I/O only will make the code easier to follow.If you need to pass an instance of either of the classes that you created, pass it by reference.