2011-05-05 112 views
0

我的教授指示我们制作一个星巴克菜单,用户可以继续输入订单,直至完成。我把菜单和循环一起显示下来,但我无法得到它加总输入的订单并显示总计。帮助实施“商店购买”计划

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

任何可以帮助我的信息?我只是一个初学者,并且已经尝试了几个小时。

回答

1

在伪代码,你想是这样的:

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阵列以外的范围。

0

你有你的代码中设置了权证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. 
1

你可能在寻找的东西是这样的:

#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'处用'0.00'值偏移您的arr。 2)成本加在你的索引数组之后,因此你可能想要根据你的数组格式化你的输出,所以下一次,你需要做的就是更新你的数组。

希望它有帮助。干杯!