餐厅计费程序功能

时间:2014-11-10 01:27:08

标签: c++ function billing

我需要制作一个餐馆账单计算器程序,允许人们从菜单项(一个功能)列表中选择,直到他们拥有他们想要订购的所有东西,然后计算他们从列表中选择完成后的总数。然后它需要他们投标的金额并减去总加税和小费来计算变化。

我在这里和其他地方找到了几个想法和类似的程序,但没有给我一个足够好的想法如何最终确定。我有程序编码但我无法弄清楚如何获取运行总计并继续累积它直到用户输入“8”。我有一个正常运行的程序,但是在每次选择后总计而不是保持运行总计并在用户点击“8”键结束时计算它。

看看下面,看看你是否可以指出我正确的方向。基本上这个赋值是关于函数的,所以我们被要求使用函数来显示菜单并计算总数。

#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 个答案:

答案 0 :(得分:2)

&#34;平衡&#34;由showFees函数计算。所以,您的问题是您需要在后续调用中维护showFees中的状态(某些数据)。你可以做到这一点的最好方法是使用OOP。当您使用过程范例在C ++中编程时,我将向您介绍过程编程中可用的一些解决方案。

  1. 全局变量
  2. 你可以有一个全局变量来保存总数。这是您可以拥有的最简单,最直观的worst解决方案。唐&#39;吨

    1. 静态变量
    2. 您可以在showFees中拥有一个存储当前总数的静态变量。比全局变量更好,但仍然是bad

      1. 将总数存储在主
      2. 创建一个表示总数的变量,将其初始化为0并创建第三个参数showFees,该变量带有指向double的指针。这样,在showFees函数调用结束后,对该变量所做的更改将保留。在C ++中,您也可以使用引用(这是C ++中推荐的方法)。

        1. 改进您的计划
        2. 在编程中有一个称为模块化的概念。使用功能,您不会有重复的代码。但是一个函数应该只做一件事,尽可能做到最好。这样,您的功能更小,更易于管理。在showFees中你做了两件事:计算一些财务事物并产生输出。这应该总是分开的。计算或business logic应该在一个函数中完成(可以按照我上面描述的方式工作),另一个函数中的输出生成或视觉逻辑。

          当然,这是一个小程序,我谈到的分离可能是一种矫枉过正。但是,您可以考虑改进功能,使其尽可能模块化。