密码尝试限制

Password attempt limit

本文关键字:密码      更新时间:2023-10-16

这里有一个c++新手。

如何限制在这个程序中输入密码的次数?到目前为止,它会重新打印,直到输入正确的密码。我想设置3次尝试的限制,并执行"尝试限制达到"或类似的东西。

{
string password;
while (true)
{
    cout << "Please enter your password.n";
    cin >> password;
    if (password == "password")
    {
        cout << "Welcome!n";
        break;
    }
   cout << "Please try again.n";
   }
}

提前感谢,

杰斯。

您可以使用for循环:

string password;
//Ask for a password as long as the user hasn't incorrectly entered one three or more times
for (int tries = 0; tries < 3; ++tries)
{
    cout << "Please enter your password.n";
    cin >> password;
    if (password == "password")
    {
        cout << "Welcome!n";
        break;
    }
    cout << "Please try again.n";
}
//If the password limit was reached without a correct password, send them an error message
if (password != "password") cout << "Sorry, try again later.n";