我是c ++的新手,我在完成任务时遇到了麻烦。我在编译时需要帮助解决错误,如何计算税金和应付金额。这是作业......
编写一个程序,帮助当地餐厅自动化其早餐计费系统。该计划应该做到以下几点: 一个。向顾客展示餐厅提供的不同早餐食品。 湾允许客户从菜单中选择多个项目 C。计算并打印账单 假设餐厅提供以下早餐食品(每件商品的价格显示在商品右侧):
普通蛋$ 1.45 培根和鸡蛋$ 2.45 松饼$ 0.99 法式吐司1.99美元 水果篮$ 2.44 谷物0.69美元 咖啡0.50美元 茶$ 0.75
首先,定义一个struct,menuItemType,它包含两个组件:string类型的menuItem和double类型的menuPrice。 然后使用struct menuItemType的数组,称之为menuList。您的程序必须包含以下功能: •函数getData:此函数将数据加载到数组menulist中。 •功能showMenu:此功能显示餐厅提供的不同项目,并告诉用户如何选择项目。 •功能printCheck:此功能计算并打印支票。 (请注意,结算金额应包含5%的税)
示例输出:
欢迎来到约翰尼餐厅 培根和鸡蛋$ 2.45 松饼$ 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 ==========
答案 0 :(得分:1)
现在终于可以了......
它有错误,例如 -
1。通过赋值运算符尝试复制字符串。您应该使用strcpy()
。
2。在本地声明变量并在全局使用它们。
3。有打字的错误。
4。该代码不包含最后要打印的净金额的声明。
5。 TAX从未使用过,然后我终于使用了最终结果显示的地方。
6。 while和switch在函数/方法之外声明。
#include <iostream>
#include <string>
#include <iomanip>
#include <string>
using namespace::std;
struct menuItemType
{
char menuItem[20];
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;
menuItemType menuList[8];
menuItemType orderList[MAX];
int main()
{
int orderCounter = 0;
int selection = ' ';
getData(menuList);
showMenu(menuList, orderList, orderCounter, selection);
printCheck(orderList, orderCounter, TAX);
return 0;
}
void getData(menuItemType menuList[8])
{
strcpy(menuList[0].menuItem,"Plain Egg");
menuList[0].menuPrice = 1.45;
strcpy(menuList[1].menuItem,"Bacon and Egg");
menuList[1].menuPrice = 2.45;
strcpy(menuList[2].menuItem,"Muffin");
menuList[2].menuPrice = 0.99;
strcpy(menuList[3].menuItem,"French Toast");
menuList[3].menuPrice = 1.99;
strcpy(menuList[4].menuItem,"Fruit Basket");
menuList[4].menuPrice = 2.49;
strcpy(menuList[5].menuItem,"Cereal");
menuList[5].menuPrice = 0.69;
strcpy(menuList[6].menuItem,"Coffee");
menuList[6].menuPrice = 0.50;
strcpy(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))
{
strcpy(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 (orderCounter = 0; orderCounter < MAX; orderCounter++)
cout << orderList[orderCounter].menuItem << " "<< orderList[orderCounter].menuPrice<< endl;
cout << "Tax" << setw(10) << TAX<<endl;
cout << "Amount Due" << setw(10) << endl;
}
以上代码已得到纠正,但逻辑错误仍然存在,您应该查看它们,因为您没有在代码中提到您的逻辑。 amount
仍未显示,因为您尚未为其添加代码!
答案 1 :(得分:1)
你在main中初始化了orderCounter但你没有在任何函数中返回它,所以最后它仍然是0.同样的事情是数组。除非通过引用或指针传递参数,否则c ++中的函数正在处理传递数据的副本。在您的情况下,您可能希望对orderCounter和结构数组使用全局声明。它不是最好的解决方案,但它有效。或者,如果您想坚持使用当前代码,请阅读一些关于函数和传递参数的内容。 http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/