此题比较简单,实现中有注释需注意的地方。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string pwd; // 正确密码
int n; // 错误次数
cin >> pwd >> n;
string temp;
getline(cin, temp); //消除cin 使用后的回车
string pwd1; // 接受输入的密码
do{
getline(cin, pwd1);
n--;
if (pwd1 == "#") //测试点2. 读到‘#’字符时结束输入
break;
if (n >= 0 && pwd1 == pwd){
cout << "Welcome in";
break;
}
else if (n > 0 && pwd1 != pwd){
cout << "Wrong password: " << pwd1 << endl;
}
if (n == 0 && pwd1 != pwd){
cout << "Wrong password: " << pwd1 << endl;
cout << "Account locked";
break;
}
} while (pwd1!="#");
return 0;
}