2011-05-05 91 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)成本加在你的索引數組之後,因此你可能想要根據你的數組格式化你的輸出,所以下一次,你需要做的就是更新你的數組。

希望它有幫助。乾杯!