C++ C+中的getline和while错误+;

C++ C+中的getline和while错误+;,c++,C++,您好,我是C++的初学者,下面是我的练习代码: cout << "Hello World! "<< "Please enter your name" << " "; //cin >> i; getline(cin, mystring); stringstream(mystring) >> name; cout << "Please enter your p

您好,我是C++的初学者,下面是我的练习代码:

cout << "Hello World! "<< "Please enter your name" << " ";
//cin >> i;
getline(cin, mystring);
stringstream(mystring) >> name;
cout << "Please enter your password"<< " " ;
getline(cin, mystring);
stringstream(mystring) >> password;
cout << password << endl;

do {
    cout << "Sorry, your password does not match your name, please enter the password again" << " " << endl;
    getline(cin, mystring);
    stringstream(mystring) >> password;
} while (!(name == "victor" & password == "victor"));
cout>name;
密码;

coutA
do…while
循环始终至少执行一次,因为底部的测试是在循环结束时执行的

因此,您至少会看到一次关于错误密码的消息。如果只想在条件为真时执行循环,请使用规则的
while
循环,条件位于顶部

此外,逻辑和运算符是
&
,而不是
&

while (!(name == "victor" && password == "victor"));
{
    cout << "Sorry, your password does not match your name, please enter the password again" << " " << endl;
    getline(cin, mystring);
    stringstream(mystring) >> password;
}
while(!(name==“victor”&password==“victor”);
{

您是否也应该将while替换为!(name==password)。这样,它可以与任何name@Mitchel0022,谢谢你的建议。但我想在这里有一个密码名单,这样我就可以让我认识的人登录系统。但是,谢谢!顺便说一句,为什么要投否决票?