首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++“自动售货机”任务

C++“自动售货机”任务
EN

Code Review用户
提问于 2016-10-07 11:45:30
回答 1查看 19.5K关注 0票数 4

任务:

编写一个程序,让用户选择你最喜欢的5种饮料(可乐,水,雪碧,……等等)。然后允许用户通过输入数字1-5来选择饮料.输出他们选择的饮料。★如果编程使用If语句而不是开关语句,则修改它以使用switch语句。如果您的程序使用开关语句,则修改它以使用If / modify语句。★★修改程序,以便如果用户输入的选择不是1-5,那么它将输出“Error.select无效,这是您的钱回来”。

代码语言:javascript
复制
#include <iostream>
using namespace std;

int main() {

int drink = 0;
string ifswitch;
string again = "y";


while( again == "y" || again == "Y")
{
    cout << "Would you like to use if or switch statements?: ";
    cin >> ifswitch;
    cout << endl;

    if(ifswitch == "switch" || ifswitch == "Switch")
    {
        cout << "Now using switch statements..." << endl << endl;

        cout << "Your choices of drinks are: " << endl;
        cout << "1 - Coke" << endl;
        cout << "2 - Sprite" << endl;
        cout << "3 - Water" << endl;
        cout << "4 - Orange Juice" << endl;
        cout << "5 - Apple Juice" << endl;

        cout << "What drink would you like (1/5): ";
        cin >> drink;
        cout << endl;

        switch(drink)
        {
            case 1 :
                cout << "You chose Coke." << endl;
                break;
            case 2 :
                cout << "You chose Sprite." << endl;
                break;
            case 3 :
                cout << "You chose Water." << endl;
                break;
            case 4 :
                cout << "You chose Orange Juice." << endl;
                break;
            case 5 :
                cout << "You chose Apple Juice." << endl;
                break;  
            default :
                cout << "Error. Choice was not valid, Here is your money back.";

        }   
    }
    else if(ifswitch == "if" || ifswitch == "If")
    {
        cout << "Now using if statements..." << endl << endl;

        cout << "Your choices of drinks are: " << endl;
        cout << "1 - Coke" << endl;
        cout << "2 - Sprite" << endl;
        cout << "3 - Water" << endl;
        cout << "4 - Orange Juice" << endl;
        cout << "5 - Apple Juice" << endl;

        cout << "What drink would you like (1/5): ";
        cin >> drink;
        cout << endl;

        if(drink == 1)
            cout << "You chose Coke." << endl;
        else if(drink == 2)
            cout << "You chose Sprite." << endl;
        else if(drink == 3)
            cout << "You chose Water." << endl;
        else if(drink == 4)
            cout << "You chose Orange Juice." << endl;
        else if(drink == 5)
            cout << "You chose Apple juice." << endl;
        else
        cout << "Error. Choice was not valid, Here is your money back.";        

    }   

    cout << "Would you like to go again? (Y/N)" << endl;
    cin >> again;
    cout << endl;

}

    cout << "Ending...";

}
EN

回答 1

Code Review用户

发布于 2016-10-07 14:07:38

这里有一种方法可以避免编写两个版本的指令,既不使用switch也不使用级联if

代码语言:javascript
复制
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

static const std::vector<std::string> drinks = {
    "Coke",
    "Sprite",
    "Water",
    "Orange Juice",
    "Apple Juice"
};

void print_drinks(auto& o, std::vector<auto> drinks)
{
    int i = 0;
    for (auto drink: drinks)
        o << ++i << " - " << drink << "\n";
    o.flush();
}

int main()
{
    print_drinks(std::cout, drinks);

    std::cout << "What drink would you like? (1-" << drinks.size() << ")" << std::endl;
    size_t n;
    if (!(std::cin >> n) || !n || n > drinks.size()) {
        std::cerr << "Error. Choice was not valid, Here is your money back." << std::endl;
        return EXIT_FAILURE;
    }

    // We now have a valid selection
    const std::string drink = drinks[n-1];
    std::cout << "You chose " << drink << "." << std::endl;

    // Implicit EXIT_SUCCESS
}

我不建议你把这个作为你的家庭作业,但是你可以从学习中学到一些东西。

  • 我们没有在任何需要的地方重复饮料的名字,而是把它们存放在一个地方。这确保了打印的菜单与选择后的输出之间的一致性(例如,在您的代码中,您有Apple JuiceApple juice)。它还可以添加或删除饮料,而无需更改代码的任何“逻辑”部分。
  • 按照“做好一件事”的理念,我已经把周围的循环取出来了。您可以很容易地使用shell来重复执行程序: while ./select_ done;do :;done或while;do ./select_done;done或do ./select_done;同时阅读-p“您还想再去吗?y/N”-N 1再次\& [ $again =~ [yY] ]
票数 0
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/143528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档