我是C ++的新手,我在分配作业时遇到麻烦。在编译时,我还需要有关错误的帮助,以及如何进行应纳税额和总金额的计算。这是作业...
编写程序来帮助本地餐馆自动化其早餐计费系统。该程序应执行以下操作:
一种。向顾客展示餐厅提供的不同早餐。
b。允许客户从菜单中选择一项以上
C。计算并打印账单
假设餐厅提供以下早餐项目(每个项目的价格显示在项目的右侧):
原蛋$ 1.45
$ 2.55培根和鸡蛋
松饼$ 0.99
法式吐司1.99
水果篮$ 2.44
谷物0.69美元
咖啡$ 0.50
茶$ 0.75
首先,定义一个结构,menuItemTypeType,具有两个组件:string类型的menuItem和double类型的menuPrice。
然后使用结构menuItemType的数组,将其称为menuList。您的程序必须包含以下功能:
•函数getData:此函数将数据加载到阵列菜单列表中。
•函数showMenu:此功能显示餐厅提供的不同物品,并告诉用户如何选择这些物品。
•函数printCheck:此函数计算并打印支票。 (请注意,帐单金额应包含5%的税)
样本输出:
欢迎来到约翰尼的餐厅
$ 2.55培根和鸡蛋
松饼$ 0.99
咖啡$ 0.50
税金$ 0.20
应付金额$ 4.14
到目前为止是我的代码
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct menuItemType
{
string menuItem;
double menuPrice;
};
void getData(menuItemType menuList[8]);
void showMenu(menuItemType menuList[8], menuItemType orderList[], int orderCounter, int selection);
void printCheck(menuItemType menuList[8], int orderCounter, const double TAX);
const int MAX = 8;
const double TAX = 1.05;
int main()
{
int orderCounter = 0;
int selection = ' ';
menuItemType menuList[8];
menuItemType orderList[MAX];
getData(menuList);
showMenu(menuList, orderList, orderCounter, selection);
printCheck(orderList, orderCounter, TAX);
return 0;
}
void getData(menuItemType menuList[8])
{
menuList[0].menuItem = "Plain Egg";
menuList[0].menuPrice = 1.45;
menuList[1].menuItem = "Bacon and Egg";
menuList[1].menuPrice = 2.45;
menuList[2].menuItem = "Muffin";
menuList[2].menuPrice = 0.99;
menuList[3].menuItem = "French Toast";
menuList[3].menuPrice = 1.99;
menuList[4].menuItem = "Fruit Basket";
menuList[4].menuPrice = 2.49;
menuList[5].menuItem = "Cereal";
menuList[5].menuPrice = 0.69;
menuList[6].menuItem = "Coffee";
menuList[6].menuPrice = 0.50;
menuList[7].menuItem = "Tea";
menuList[7].menuPrice = 0.75;
}
void showMenu(menuItemType menuList[8], menuItemType orderList[], int orderCounter, int selection)
{
cout << "1 - Plain Egg" << setw(14) << "$1.45" << endl;
cout << "2 - Bacon and Egg" << setw(10) << "$2.45" << endl;
cout << "3 - Muffin" << setw(17) << "$0.99" << endl;
cout << "4 - French Toast" << setw(11) << "$1.99" << endl;
cout << "5 - Fruit Basket" << setw(11) << "$2.49" << endl;
cout << "6 - Cereal" << setw(17) << "$0.69" << endl;
cout << "7 - Coffee" << setw(17) << "$0.50" << endl;
cout << "8 - Tea" << setw(21) << "$0.75\n" << endl;
}
do{
cout << "Enter the number of your selection until you have completed your order." << endl;
cout << "Enter 9 when you have finished." << endl;
cin >> selection;
Switch(selection)
{
case 9:
break;
case 1:
cout << menuList[0].menuItem << setw(14) << "$1.45";
break;
case 2:
cout << menuList[1].menuItem << setw(10) << "$2.45";
break;
case 3:
cout << menuList[2].menuItem << setw(17) << "$0.99";
break;
case 4:
cout << menuList[3].menuItem << setw(11) << "$1.99";
break;
case 5:
cout << menuList[4].menuItem << setw(11) << "$2.49";
break;
case 6:
cout << menuList[5].menuItem << setw(17) << "$0.69";
break;
case 7:
cout << menuList[6].menuItem << setw(17) << "$0.50";
break;
case 8:
cout << menuList[7].menuItem << setw(20) << "$0.75";
break;
default:
cout << "Invalid Selection! Selections must be between 1 and 9\n";
}
}
{
while (selection !=9);
if((selection >=0) && (selection <= MAX))
{
orderList[orderCounter].menuItem = menuList[selection-1].menuItem;
orderList[orderCounter].menuPrice = menuList[selection-1.menuPrice;
orderCounter++;
}
else if (selection == 9)
cout << "Exit" << endl;
else
cout << "Invalid Entry" << endl;
}
}
void printCheck(menuItemType orderList[], int orderCounter, const double TAX)
{
cout << "Welcome to the Breakfast Place!"<< endl;
for (int corderCounter = 0; orderCounter < MAX; orderCounter++)
cout << orderList[orderCounter].menuItem << " "
<< orderList[orderCounter].menuPrice<< endl;
cout << "Tax" << setw(10) << endl;
cout << "Amount Due" << setw(10) << endl;
}
这是我尝试调试时的错误
> BreakfastBilling.cpp
1>breakfastbilling.cpp(69): error C2059: syntax error : 'do'
1>breakfastbilling.cpp(69): error C2143: syntax error : missing ';' before '{'
1>breakfastbilling.cpp(69): error C2447: '{' : missing function header (old-style formal list?)
1>breakfastbilling.cpp(106): error C2447: '{' : missing function header (old-style formal list?)
1>breakfastbilling.cpp(111): error C2059: syntax error : 'bad suffix on number'
1>breakfastbilling.cpp(119): error C2059: syntax error : '}'
1>breakfastbilling.cpp(119): error C2143: syntax error : missing ';' before '}'
1>breakfastbilling.cpp(119): error C2059: syntax error : '}'
1>breakfastbilling.cpp(121): error C2143: syntax error : missing ';' before '{'
1>breakfastbilling.cpp(121): error C2447: '{' : missing function header (old-style formal list?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
最佳答案
您在main中初始化了orderCounter,但没有在任何函数中返回它,因此最后它仍为0。数组也是如此。除非参数是通过引用或指针传递的,否则c ++中的函数将对传递的数据进行复制。在您的情况下,您可能想对orderCounter和结构数组使用全局声明。它不是最好的解决方案,但可以。或者,如果您想坚持当前的代码,请阅读一些有关函数和传递参数的内容。 http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/