c++ - 帮助实现一个 "store buying"程序

标签 c++

我的教授指示我们制作一个类似星巴克的菜单,用户可以在其中继续输入订单直到完成。我在循环中显示了菜单,但无法将输入的订单相加并显示总数。

#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 数组范围之外的内容。

关于c++ - 帮助实现一个 "store buying"程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5892491/

相关文章:

c++ - gcc 是否保证对 volatile 整数的对齐访问是原子的?

c++ - 不同的 CCLayer 大小取决于它在 block 内的引用位置

c++ - 即使在使用 skip_permission_denied 时,std::filesystem 递归迭代器也会抛出 permission_denied

c++ - std::mutex 会创建栅栏吗?

c++ - 带有 openframeworks/opengl 的色度键

c++ - 将 char** 转换为 char[x][x]

c++ - 简单程序在并行化后不工作

c++实现全局开关/标志以控制程序行为而无需将类绑定(bind)到公共(public)点的最佳方法

c++ - 是否可以在一个函数中使用两次 va_list 方法?

python - 从 C++ 函数 Cython 返回包含 PyObject 的复杂对象