咖啡订购系统使用switch语句

时间:2016-10-26 21:53:47

标签: c++

美好的一天!我正在尝试使用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;
}

1 个答案:

答案 0 :(得分: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;