如何阻止程序跳过 getline? [复制]

2024-03-25

这是我的主要程序,

int main () {

    string command;
    cin>>command;

    if(command == "keyword")
    {
        string str, str2, str3, str4;

        cout << "Enter first name: ";
        getline (cin,str);

        cout << "Enter last name: ";
        getline (cin,str2);

        cout << "Enter age: ";
        getline (cin,str3);

        cout<<"Enter country: ";
        getline (cin,str4);

        cout << "Thank you, " << str <<" "<<str2 <<" "<<str3<<" "<<str4<< ".\n";
    }
}

当输入关键字时,程序立即输出:

输入名字: 输入姓氏:

完全绕过输入名字的能力。


string command;
cin>>command;

在此之后,只吃该行的末尾

string restOfLine;
getline(cin, restOfLine);

否则,您输入命令的行中的“\n”不会被消耗,下一个读取行只会读取它。华泰

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何阻止程序跳过 getline? [复制] 的相关文章

随机推荐