【发布时间】:2014-11-10 01:27:08
【问题描述】:
我需要制作一个餐厅账单计算器程序,它允许人们从菜单项列表中进行选择(一个函数),直到他们拥有他们想要订购的所有东西,然后在他们从列表中完成选择后计算总数.然后用他们投标的金额减去加税和小费的总和来计算零钱。
我在这里和其他地方找到了一些想法和类似的程序,但没有什么能让我对如何完成这个有足够的了解。我已经对程序进行了编码,但我不知道如何获取运行总计并继续累积它,直到用户输入“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;
}
【问题讨论】:
-
你是对的。我应该更好地校对。 “在每次选择之后,而不是保持运行总数并计算用户按“8”结束订单的时间。
-
用户在索取投标金额之前不应该先选择他想要的所有东西吗?然后当用户完成时,你执行
showFees()函数? -
是的。这就是我遇到的问题。我不确定如何在汇总之前存储要调用的选择。