2011-05-05 2 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; 
} 

나를 도울 수있는 정보가 있습니까? 나는 단지 초보자이며 몇 시간 동안 이것을 시도해 왔습니다. 당신은 각 항목의 비용을 포함하는 배열 이름 costs을 정의해야합니다

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

} 

: 의사 코드에서

답변

1

당신이 뭔가를 할 수 있습니다. 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'값에서 arr을 '0.00'값으로 오프셋해야합니다. 2) 추가 된 비용은 색인 된 배열의 비용을 따르므로 배열에 따라 출력의 형식을 지정해야하므로 다음에 배열을 업데이트하기 만하면됩니다.

희망이있었습니다. 건배!

관련 문제