为什么我的开关案例被跳过并默认打印?

时间:2016-02-05 19:48:23

标签: c++

我编写了这个C ++代码,试图模拟自动售货机。 无论何时输入用户的选择,每个在switch语句中分配一个案例1-5,没有一个案例执行:程序不识别有效输入,并且只打印出默认的case语句。为什么会这样?

#include <iostream>

using namespace std;

int main()
{
    int choice;
    double dollars;

    cout << "==============================================================\n";
    cout << "*                                                            *\n";
    cout << "*              VENDING MACHINE SIMULATOR                     *\n";
    cout << "*                                                            *\n";
    cout << "==============================================================\n";

    cout << "Your Choices Are:\n";

    cout << "1) Coke     - $1.75\n";
    cout << "2) Pepsi    - $2.25\n";
    cout << "3) Sprite   - $1.25\n";
    cout << "4) 7-up     - $1.15\n";
    cout << "5) Water    - $2.20\n";

    // Ask for user's selection.

    cout << "What would you like to purchase today?\n";
    cin >> choice;
    switch (choice)
    {
            case '1': 
            {
                    cout << "How many dollar bills will you use?\n";
                    cin >> dollars;
                    {
                            if (dollars > 1.75)
                            {
                                    cout << "You purchased: Coke\n";
                                    cout << "Your change is: $"<< (dollars - 1.75);
                            }
                            else
                            {
                                    cout << "You do not have enough money for this purchase. Goodbye!\n";
                            }
                    }
            }
                      break;
            case '2':
            {
                    cout << "How many dollar bills will you use?\n";
                    cin >> dollars;
                    {
                            if (dollars > 2.25)
                            {
                                    cout << "You purchased: Pepsi\n";
                                    cout << "Your change is: $"<< (dollars - 1.75);
                            }
                            else
                            {
                                    cout << "You do not have enough money for this purchase. Goodbye!\n";
                            }
                    }
            }
            case '3': 
            {
                    cout << "How many dollar bills will you use?\n";
                    cin >> dollars;
                    {
                            if (dollars > 1.25)
                            {
                                    cout << "You purchased: Sprite\n";
                                    cout << "Your change is: $"<< (dollars - 1.25);
                            }
                            else
                            {
                                    cout << "You do not have enough money for this purchase. Goodbye!\n";
                            }
                    }
            }
                      break;
            case '4': 
            {
                    cout << "How many dollar bills will you use?\n";
                    cin >> dollars;
                    {
                            if (dollars > 1.15)
                            {
                                    cout << "You purchased: 7-up\n";
                                    cout << "Your change is: $"<< (dollars - 1.15);
                            }
                            else
                            {
                                    cout << "You do not have enough money for this purchase. Goodbye!\n";
                            }
                    }
            }
                      break;
            case '5': 
            {
                    cout << "How many dollar bills will you use?\n";
                    cin >> dollars;
                    {
                            if (dollars > 2.20)
                            {
                                    cout << "You purchased: Water\n";
                                    cout << "Your change is: $"<< (dollars - 2.20);
                            }
                            else
                            {
                                    cout << "You do not have enough money for this purchase. Goodbye!\n";
                            }
                    }
            }

                      break;
            default: cout << "Your choice is INVALID. Goodbye!\n"; 
     }
    return 0;
}

4 个答案:

答案 0 :(得分:8)

删除case语句中使用的单引号。

更改

 case '1':

case 1: 

与其他人相似。

'1'是一个字符常量,而1是整数常量。使用ASCII编码时,'1'计算的数字为49,显然不等于数字1。因此,当您输入case:作为输入时,不会执行1分支。

答案 1 :(得分:3)

'1'是一个角色。当您将1输入到整数时,它将存储为1而不是'1'。您需要将'x'的所有案例更改为x

您没有收到任何错误的原因是'1'可以隐式转换为int,并且它具有实现字符编码的值。如果是ASCII,则为49

答案 2 :(得分:2)

两种解决方案:

int choice更改为char choice

或者

case '1':更改为case 1:

答案 3 :(得分:1)

案例2缺少

'break'语句并将案例'1'更改为案例1。