帮助实施"store buying"计划

Help implementing a "store buying" program

本文关键字:buying 计划 store 帮助      更新时间:2023-10-16

我的教授指示我们制作一个类似星巴克的菜单,用户可以继续输入订单,直到他们完成。我让菜单显示与循环一起下降,但我无法让它将输入的订单相加并显示总计。

#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.