Cpp switch statement won't output conditions set

时间:2019-01-09 21:44:41

标签: c++ switch-statement output

I'm running into a bit of an issue on a practice program from the cpp site. The prompt is:

Write a program that presents the user w/ a choice of your 5 favorite beverages (Coke, Water, Sprite, ... , Whatever). Then allow the user to choose a beverage by entering a number 1-5. Output which beverage they chose.

★ If you program uses if statements instead of a switch statement, modify it to use a switch statement. If instead your program uses a switch statement, modify it to use if/else-if statements.

When I compile and run, there's no output after user input.

 #include <iostream>

//cola machine

main ()
{
    //intro and options
    std::cout << "Welcome. What will you have?";
    std::cout << std::endl;
    std::cout << "1 - Coke";
    std::cout << std::endl;
    std::cout << "2 - Pepsi";
    std::cout << std::endl;
    std::cout << "3 - Mtn. Dew";
    std::cout << std::endl;
    std::cout << "4 - Water";
    std::cout << std::endl;
    std::cout << "5 - Cancel";
    std::cout << std::endl;
    //choice
    int choice;

        std::cin >> choice;

    switch (choice)
        {
            case '1':
                std::cout << "Coke";
                break;
            case '2':
                std::cout << "Pepsi";
                break;
            case '3':
                std::cout << "Mt. Dew";
                break;
            case '4':
                std::cout << "Water";
                break;
            case '5':
                std::cout << "void";
                break;

            default:
    std::cout << "enjoy your ";
    std::cout << choice;
    std::cout << "!";
        }

Any guidance would be amazing and thanks for your time.

1 个答案:

答案 0 :(得分:1)

这就是您要寻找的。

#include <iostream>

//cola machine

int main()
{
    //intro and options
    std::cout << "Welcome. What will you have?";
    std::cout << std::endl;
    std::cout << "1 - Coke";
    std::cout << std::endl;
    std::cout << "2 - Pepsi";
    std::cout << std::endl;
    std::cout << "3 - Mtn. Dew";
    std::cout << std::endl;
    std::cout << "4 - Water";
    std::cout << std::endl;
    std::cout << "5 - Cancel";
    std::cout << std::endl;
    //choice
    int choice;

    std::cin >> choice;

    std::cout << "Enjoy your ";

    switch (choice)
    {
    case 1:
        std::cout << "Coke";
        break;
    case 2:
        std::cout << "Pepsi";
        break;
    case 3:
        std::cout << "Mt. Dew";
        break;
    case 4:
        std::cout << "Water";
        break;
    case 5:
        std::cout << "void";
        break;

    default:
        std::cout << "NONE SELECTED";
    }

    std::cout << "!\n";

    system("PAUSE");
}
相关问题