开发者

Help implementing a "store buying" program

My professor instructed us to make a Starbucks like menu where the user can continue to input orders until they are finished. I got the menu display down along with the loop, but I can't get it to add up the orders that were inputted and display a total.

#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?";

 开发者_JS百科   }

    cout << endl << "Thank you for choosing Hunterbucks! Come again soon.";

    return 0;
}

Any info that can help me? I'm just a beginner and have been trying this for a few hours.


In pseudo-code you want something like this:

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?";
    }

}

You'll need to define an array names costs that contains the cost of each item. You'll also want to tackle validation of the user input so that you don't erroneously attempt to read outside the range of the costs array.


You're probably looking at something like this:

#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;
}

Do note the following:

1) Your menu choices being with '1' thus there is a need to offset your arr at index '0' with the '0.00' value there. 2) The cost added up follows that of your indexed array, thus you would probably want to format your output according to your array, so that next time, all you need to do is to update your array.

Hope it helped. Cheers!


The way you have your code set up warrants a switch statement, like the following:

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;

That being said, the easiest way to do this would be to have an array of prices:

double prices[] = {1.50, 1.23, 2.25};

// ...

total += prices[choice - 1]; // No switch statement needed.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜