C++ 为什么用户不能给出他们的输入

C++ 为什么用户不能给出他们的输入,c++,C++,这是我的密码 #include <iostream> #include <string> using namespace std; int main() { cout << "Welcome to Starbucks! What would you like to order?" << endl; cout << "Before you select your o

这是我的密码

#include <iostream>
#include <string>
using namespace std;

int main() {

    cout << "Welcome to Starbucks! What would you like to order?" << endl;
    cout
            << "Before you select your order, would you like to look at your wallet to see how much money you have on you right now? "
            << flush;

    string input;
    cin >> input;

    if (input == "Yes" || input == "yes") {
        cout << " " << endl;
        cout << "OK. You have $18.90 in your wallet. Now showing menu. "
                << flush;
    }
    else {
        cout << " " << endl;
        cout << "OK. Now showing menu. " << endl;
    }

    cout << "\n\n\tDrinks:\n\nHot Coffees\nHot Teas\nHot Drinks\nFrappuccino Blended Beverages\nCold Coffees\n\n\tFood:\n\nHot Breakfast"
            "\nBakery\nLunch\nSnacks & Sweets\nYogurt & Custard\n\n\tMerchandise:\n\nDrinkware" << endl;

    string option;

    cout << "Select an option from one of these sections. Type your selection here: " << flush;
    getline(cin, option);

    if (option == "Hot Coffees" || option == "hot coffees" || option == "Hot coffees" || option == "hot Coffees") {

        cout << "Welcome to the Hot Coffees section. Here is a list of all the hot coffees we sell:\n\nAmericanos:\n\nCaffè Americano\nStarkbucks Blonde Caffè Americano\n" << endl;
    }



    return 0;
}
#包括
#包括
使用名称空间std;
int main(){

CUT< P>这是因为C++没有考虑空格,所以尝试键入<代码> CIN。IGRANE();在GETLIN函数之后,<代码>。IF部分中出错,键入这个……/P>
if (option == "Hot Coffees" || "hot coffees" || "Hot coffees" || "hot Coffees") {

    cout << "Welcome to the Hot Coffees section. Here is a list of all the hot coffees we sell:\n\nAmericanos:\n\nCaffè Americano\nStarkbucks Blonde Caffè Americano\n" << endl;
}
if(选项==“热咖啡”| | |“热咖啡”| | |“热咖啡”){

cout很可能您的输入缓冲区中仍有一个\n,因此当您到达getline()时,它会将其读取为定界字符,并将其作为输入返回给您。我认为std::flush可能会执行与您认为不同的操作。这是一个有用的阅读:

这里有一行代码,在处理C++中控制台上的用户输入时很有用,它获取输入缓冲区中的每个字符,直到找到\n</p>

while (cin.get() != '\n');

祝你好运!

这是否回答了你的问题?我应该如何在我的代码中使用它?或者我应该如何使用它?我对cin后的get行(cin,s)不是很熟悉。你遇到了链接问题(及其副本)中描述的完全相同的问题,因此那里的任何答案都适用-使用
get()
/
get(c)
skipws
ignore
getline
两次等等,请看,我的主要问题是,我想将存储在“option”中的用户输入用于多个不同的if语句。例如,if(option==热咖啡),我想转到热咖啡部分。if(option==热茶),然后我想去热茶区。我想对上面“cout”中列出的列表中的每一项都这样做。但它就是不起作用。尽管cin.ignore();允许我在控制台中键入“热咖啡”,但它没有打印“欢迎来到热咖啡区”之后,这就是我编写的if语句所要做的。感谢您的尝试!:)
string HCoffees;cout但什么也没发生,它只留下一个空白。我做错什么了吗?哥们!我明白了!您在代码中犯了错误!检查上面答案的编辑部分