2016-10-26 6 views
-1

Good Day!私は、switch文を使ってコーヒーオーダーステーションを作ろうとしています。しかし、私は1つのケースのステートメントの下で複数の計算を行う方法を考え出すのに苦労しています。私はそれが可能かどうかわからない。また、私が入力する任意のサイズ、私はいつも同じ合計量を取得します。ご協力いただきありがとうございます。スイッチ文を使用したコーヒーオーダーシステム

ここに私のメインコードです。

#include<iostream> 
using namespace std; 
int main() 
{ 
int q, s, a; 
int js=145, jv=165, jg=185, os=130, ov=160, og=175, bs=80, bv=120,bg=150, cs=130, cv=150, cg=185; 
char d; 

cout <<"MENU:" <<endl <<endl; 
cout <<"[J] Java Chips (Small 145) (Venti 165) (Grande 185)" <<endl; 
cout <<"[O] Oreo Cookies & Cream (Small 130) (Venti 160) (Grande 175)" <<endl; 
cout <<"[B] Brewed Coffee (Small 80) (Venti 120) (Grande 150)" <<endl; 
cout <<"[C] Caramel Macchiato (Small 130) (Venti 150) (Grande 185)" <<endl <<endl; 
cout <<"Enter choice: "; 
cin >>d; 

switch(d){ 

case 'J': 
case 'j': 
    cout <<"Enter Quantity: "; 
    cin >>q; 
    cout <<"Enter Size: "; 
    cin >>s; 
    a = q * js; 
    a = q * jv; 
    a = q * jg; 

    cout <<"The total amount of your order is: " <<a <<endl; 
    break; 

case 'O': 
case 'o': 
    cout <<"Enter Quantity: "; 
    cin >>q; 
    cout <<"Enter Size: "; 
    cin >>s; 
    a = q * os; 
    a = q * ov; 
    a = q * og; 
    cout <<"The total amount of your order is: " <<a <<endl; 
    break; 

case 'B': 
case 'b': 
    cout <<"Enter Quantity: "; 
    cin >>q; 
    cout <<"Enter Size: "; 
    cin >>s; 
    a = q * bs; 
    a = q * bv; 
    a = q * bg; 
    cout <<"The total amount of your order is: " <<a <<endl; 
    break; 

case 'C': 
case 'c': 
    cout <<"Enter Quantity: "; 
    cin >>q; 
    cout <<"Enter Size: "; 
    cin >>s; 
    a = q * cs; 
    a = q * cv; 
    a = q * cg; 
    cout <<"The total amount of your order is: " <<a <<endl; 
    break; 

default: 
    cout <<"Not on the menu!"; 
} 
return 0; 
} 
+0

'+ ='演算子を探します。 – user4581301

+0

私はその演算子をどのように使用するのか説明できますか? – xJohn

+0

[信頼できる文書を素早く指し示す](http://en.cppreference.com/w/cpp/language/operator_precedence) – user4581301

答えて

0

商品数量と商品価格を取得したいと思う。数量に価格を掛けます:

case 'j': 
    cout << "Enter Quantity: "; 
    cin >> q; 
    if (q == 0) 
    { 
     cout << "Quantity was zero\n"; 
     break; 
    } 

    cout << "Enter Size: (valid sizes: 1, 2, 3)"; 
    cin >> s; 
    a = 0; 
    if (s == 1) a = q * js; 
    else if (s == 2) a = q * jv; 
    else if (s == 3) a = q * jg; 
    else cout << "You entered invalid size. Size should be 1, 2, or 3\n"; 

    if (a != 0) 
     cout << "The total amount of your order is: " << a << endl; 

    break; 
+0

私はあなたの提案を使用したとき、私はいつも私の総注文に0を得る。あなたはそれが私の変数宣言と関係していると思いますか? – xJohn

+0

いいえ、サイズ 's'の入力は何ですか? (有効なサイズは1,2、または3です)更新された回答を参照してください。 –

+0

私はすでにそれを得ました。ご協力ありがとうございました。 – xJohn

関連する問題