Restaurant Billing Program Function

26.4k views Asked by At

I need to make a restaurant bill calculator program that allows people to choose from a list of menu items (a function) until they have everything they've wanted to order and then calculate the total when they are finished selecting from a list. Then it takes the amount they tender and subtracts the total plus tax and tip to calculate change.

I've found several ideas and similar programs here and on other places but nothing that has given me a good enough idea of how to get this finalized. I have the program coded but I can't figure out how to take the running total and keep accumulating it until the user enters "8". I have a functioning program but it totals after each selection instead of keeping a running total and calcuating it when the user hits the "8" key to end.

Take a look below and see if you can point my in the right direction if you would. Basically this assignment is about functions so we're asked to use functions to display the menu and calculate the total.

#include <iostream>
#include <iomanip>
using namespace std;

// Function Prototypes
void showMenu();
void showFees(double, int);

int main()
{
    int choice; //To Hold Menu Choice
    double quantity = 1;




    //contants for menu choices
    const int hamburgerChoice = 1;
    const int hotdogChoice = 2;
    const int peanutsChoice = 3;
    const int popcornChoice = 4;
    const int sodaChoice = 5;
    const int chipsChoice = 6;
    const int waterChoice = 7;
    const int endOrderChoice = 8;

    //constants for menu prices
    const double hamburger = 6.00;
    const double hotdog = 4.50;
    const double peanuts = 3.75;
    const double popcorn = 5.50;
    const double soda = 2.80;
    const double chips = 1.00;
    const double water = 2.00;

    //set precision
    cout << fixed << showpoint << setprecision(2);

    do
    {
        //display menu and get user choice
        showMenu();
        cin >> choice;

        //validate choice
        while (choice < hamburgerChoice || choice > endOrderChoice)
        {
            cout << "Please enter a valid menu choice: ";
            cin >> choice;
        }

        //if the user does not want to quit proceed
        if (choice != endOrderChoice)
        {

            //display fees
            switch (choice)
            {
                case hamburgerChoice:
                    showFees(hamburger, quantity);
                    break;
                case hotdogChoice:
                    showFees(hotdog, quantity);
                    break;
                case peanutsChoice:
                    showFees(peanuts, quantity);
                    break;
                case popcornChoice:
                    showFees(popcorn, quantity);
                    break;
                case sodaChoice:
                    showFees(soda, quantity);
                    break;
                case chipsChoice:
                    showFees(chips, quantity);
                    break;
                case waterChoice:
                    showFees(water, quantity);
                    break;

            }
                    }
    }
    while (choice != endOrderChoice);
    system("pause");
    return 0;

}
//*************************************************************
//Definition of function showMenu which displays the menu    **
//*************************************************************

void showMenu()
{
    cout << "\n\t\tBaseball Game Snacks" << endl;
    cout << "1. Hamburger \t$6.00"<< endl;
    cout << "2. Hotdog \t\t$4.50" << endl;
    cout << "3. Peanuts \t\t$3.75" << endl;
    cout << "4. Popcorn \t\t$5.50" << endl;
    cout << "5. Soda \t\t$2.80" << endl;
    cout << "6. Chips \t\t$1.00"<< endl;
    cout << "7. Water \t\t$2.00" << endl;
    cout << "8. END ORDER" << endl;
    cout << "Please enter your menu choice: ";
}

//************************************************************
//Definition of function showFees which caculates the total **
//bill                                                      **
//************************************************************

void showFees(double itemCost, int quantity)
{
    double amtTendered;
    double totalBill = (itemCost * quantity);
    double taxRate = .065;
    double tipRate = .20;
    double tip = (totalBill * tipRate);
    double tax = (totalBill * taxRate);
    double amountDue = (totalBill + tax + tip);
    cout << "Total Bill: $" << totalBill << endl;
    cout << "Tax: $" << tax << endl;
    cout << "Tip: $" << tip << endl;
    cout << "Total Amount Due: $" << amountDue << endl;
    cout << "Enter ammount tendered: $";
    cin >> amtTendered;
    double changeDue = amtTendered - amountDue;
    cout << "Change Due: $" << changeDue << endl;

}
1

There are 1 answers

0
Paul92 On

The "balance" is calculated by the showFees function. So, your problem is that you need to maintain the state (some data) in showFees in subsequent calls. The best way you could do this is using OOP. While you are programming in C++ using the procedural paradigm, I will point you some of the solutions available in procedural programming.

  1. Global variables

You could have a global variable to hold the total. This is the simplest, the most intuitive and the worst solution you could have. Don't.

  1. Static variables

You could have a static variable in showFees that stores the current total. Better than a global variable, but still bad.

  1. Store the total in main

Create a variable that represents the total, initialize it to 0 and create a third argument of showFees that takes a pointer to a double. This way, the changes done to that variable will remain after the showFees function call ends. In C++ you can use references also (this is the recommended way in C++).

  1. Improve your program

In programming there is a concept called modularity. Using functions, you don't have duplicate code. But a function should do only one thing, and do it as best as possible. This way, your functions are smaller and easier to manage. In showFees you do 2 things: compute some financial things and generate output. This should always be separated. The computations, or business logic, should be done in a function (that can work in the way I described above), and the output generation, or visual logic, in another function.

Of course, this is a small program and the separation that I talk about is probably an overkill. However, you can think at ways to improve your function so that they are as modular as possible.