Cout在getline之前写了两次

时间:2018-02-21 18:35:47

标签: c++ cin getline

所以,我已宣布string名为command

那是我的代码:

int main()
{
string username, password, command;
cout << "login as: "; cin >> username;
if (username.substr(username.length()-1) == "s")
{
    cout << username << "' password: "; cin >> username;
}
else
{
    cout << username << "'s password: "; cin >> password;
}
system("clear");
cout << "Welcome to Shell, " << username << "." << endl << "Type 'help' to see the list of commands." << endl;
cout << "" << endl;
do
{
    cout << username << "@localhost -$ ";
    getline(cin, command);
} while(1);
}

如果username的值为admin,那么输出就是

admin@localhost -$ admin@localhost -$ (here goes the cin)

而不是

admin@localhost -$ (here goes the cin)

我该怎么办?感谢。

0 个答案:

没有答案