帮助实施"store buying"计划
Help implementing a "store buying" program
我的教授指示我们制作一个类似星巴克的菜单,用户可以继续输入订单,直到他们完成。我让菜单显示与循环一起下降,但我无法让它将输入的订单相加并显示总计。
#include <iostream>
using namespace std;
int main()
{
int choice = 1;
cout << endl << "Welcome to Hunterbucks!";
while (choice > 0)
{
cout << endl << "Input -1 when you're finished ordering!";
cout << endl << endl << "Coffee" << " " << "($)";
cout << endl << "1. Regular" << " " << "1.50";
cout << endl << "2. Decaf" << " " << "1.23";
cout << endl << "3. Americano" << " " << "2.25";
cout << endl << "4. Espresso" << " " << "2.25";
cout << endl << "5. Latte" << " " << "2.50";
cout << endl << "6. Cappuccino" << " " << "2.75";
cout << endl << "7. Frappuccino" << " " << "2.75";
cout << endl << "8. Macchiato" << " " << "2.50";
cout << endl << endl << "Snacks" << " " << "($)";
cout << endl << "9. Muffin" << " " << "1.00";
cout << endl << "10. Blueberry Muffin" << " " << "1.25";
cout << endl << "11. Raspberry Muffin" << " " << "1.25";
cout << endl << "12. Scone" << " " << "0.75";
cout << endl << "13. Blueberry Scone" << " " << "1.00";
cout << endl << "14. Croissant" << " " << "0.75";
cout << endl << endl << "What would you like to order? ";
cin >> choice;
if (choice <= 0)
cout << endl << "Thank you for your order.";
else
cout << endl << "What else would you like to order?";
}
cout << endl << "Thank you for choosing Hunterbucks! Come again soon.";
return 0;
}
有什么信息可以帮助我吗?我只是一个初学者,已经尝试了几个小时。
在伪代码中,你需要这样的东西:
float total = 0.0;
while (choice > 0)
{
....
cin >> choice;
if (choice <= 0)
cout << endl << "Thank you for your order.";
else
{
total += costs[choice];
cout << endl << "What else would you like to order?";
}
}
您需要定义一个数组名称costs
其中包含每个项目的成本。您还需要处理用户输入的验证,以便不会错误地尝试读取costs
数组范围之外的内容。
你可能看到的是这样的东西:
#include <iostream>
using namespace std;
int main()
{
int choice = 1;
float sum = 0.0;
float arr[] = {
0.00, 1.50, 1.23, 2.25, 2.25, 2.50, 2.75, 2.75, 2.50,
1.00, 1.25, 1.25, 0.75, 1.00, 0.75
};
cout << endl << "Welcome to Hunterbucks!";
while (choice > 0)
{
cout << endl << "Input -1 when you're finished ordering!";
cout << endl << endl << "Coffee" << " " << "($)";
cout << endl << "1. Regular" << " " << "1.50";
cout << endl << "2. Decaf" << " " << "1.23";
cout << endl << "3. Americano" << " " << "2.25";
cout << endl << "4. Espresso" << " " << "2.25";
cout << endl << "5. Latte" << " " << "2.50";
cout << endl << "6. Cappuccino" << " " << "2.75";
cout << endl << "7. Frappuccino" << " " << "2.75";
cout << endl << "8. Macchiato" << " " << "2.50";
cout << endl << endl << "Snacks" << " " << "($)";
cout << endl << "9. Muffin" << " " << "1.00";
cout << endl << "10. Blueberry Muffin" << " " << "1.25";
cout << endl << "11. Raspberry Muffin" << " " << "1.25";
cout << endl << "12. Scone" << " " << "0.75";
cout << endl << "13. Blueberry Scone" << " " << "1.00";
cout << endl << "14. Croissant" << " " << "0.75";
cout << endl << endl << "What would you like to order? ";
cin >> choice;
if (choice <= 0){
cout << endl << "Thank you for your order.";
} else {
cout << endl << "What else would you like to order?";
sum += arr[choice];
}
}
cout << "Total: " << sum << endl;
cout << endl << "Thank you for choosing Hunterbucks! Come again soon.";
return 0;
}
请注意以下事项:
1)您的菜单选择与"1"一起,因此需要将索引"0"处的arr与"0.00"值偏移。2) 成本加起来遵循索引数组的成本,因此您可能希望根据数组格式化输出,以便下次您需要做的就是更新数组。
希望它有帮助。干杯!
您设置代码的方式需要switch
语句,如下所示:
double total = 0;
switch (choice)
{
case 1:
total += 1.50; // Regular.
break;
case 2:
total += 1.23; // Decaf.
break;
// Etc.
}
cout << endl << "Your total is " << total;
话虽如此,最简单的方法是拥有一系列价格:
double prices[] = {1.50, 1.23, 2.25};
// ...
total += prices[choice - 1]; // No switch statement needed.
相关文章:
- 如何让C++'tally up'结果并制定计划?
- C++窗口的计划问题
- 回复计划游戏结果不会显示
- 我正在努力处理多维数组输入和输出,我管理输入,但是打印无法按计划工作
- 如何从文本文件输出数据?(学生成绩单计划)
- 为什么在此计划中,我在所有情况下都获得 0%?
- Windows 并发运行时任务计划,但有例外
- C++航班预订计划
- 如何计划在将来时间运行的操作
- 自动(toCast)显式转换是否计划在未来C++标准?
- 任务计划程序库的预处理不起作用 - 多定义错误
- GLES计划资源索引/位置与统一索引/位置
- 我应该如何使用C++创建 Google 行动行动计划履行端点?
- 多米诺骨牌计划。我不知道如何将我的矢量拉到空白中以打印出来
- C++计划持续时间内(字体)资源的分配
- 是否有任何计划将"expects"添加到 std::optional?
- 我可以在计算数据计划上修复该C 程序的任何建议吗?
- 任务计划程序:设置运行级别时拒绝访问
- 基本C++计划提前结束
- 帮助实施"store buying"计划