首页 > 解决方案 > 我以前用过一种方法,现在同样的方法不起作用

问题描述

这是我的代码。

string HCoffees;
        cout
                << "\nPlease choose one of these hot coffees. Type your selection here: "
                << flush;
        while (cin.get() != '\n')
            ;
        getline(cin, HCoffees);

        cout << "\nNow at the " << HCoffees << " section.\n" << endl;

        //If the selection is a Caffe Americano...

        if (HCoffees == "Caffe Americano" || HCoffees == "caffe americano"
                || HCoffees == "Caffe americano" || HCoffees == "caffe Americano") {
            cout << "\nWhat size Caffè Americano would you like? " << flush;
            string size;
            cin >> size;
            if (size == "Tall" || size == "tall") {
                cout << "That will be 2.25.\n\nHow many Caffè Americanos (Tall size) would you like to get?" << flush;
            }
            else if (size == "Grande" || size == "grande") {
                cout << "That will be 2.65.\n\nHow many Caffè Americanos(Grande size) would you like to get?" << flush;
            }
            else if (size == "Verti" || size == "verti") {
                cout << "That will be 2.95.\n\nHow many Caffè Americanos(Verti size) would you like to get?" << flush;
            } else {
                cout << "Size not avaliable. Chose a different size: " << flush;

            }
        }

    }

即使在控制台中输入了 Caffe Americano(用户输入了此选项),它也不会打印出“您想要哪种尺寸的 Caffe Americano?” (如果键入 Caffe Americano,这就是我在第 13 行编写的程序)。我原以为我获取用户输入的方式有问题,但我错了,因为我之前已经在我的程序中使用了相同的输入代码*,并且它起作用了。请尝试解决这个问题。谢谢!

*输入代码也如上所示:

string HCoffees;
        cout
                << "\nPlease choose one of these hot coffees. Type your selection here: "
                << flush;
        while (cin.get() != '\n')
            ;
        getline(cin, HCoffees);

        cout << "\nNow at the " << HCoffees << " section.\n" << endl;

标签: c++

解决方案


推荐阅读