程序的密码

Password to a program

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

我做了一个应用程序,可以用矩阵解决一些计算问题,我想对用户进行身份验证,以便在程序启动时授予他们访问权限。

我会告诉你我已经做了什么。

int main() 
{
const string USERNAME = "claudiu";
const string PASSWORD = "123456";
string usr, pass;
cout << "Enter Username : ";
cin >> usr;
if(usr.length() < 4)
{
    cout << "Username length must be atleast 4 characters long.";
}
else 
{
    cout << "Enter Password : ";
    cin >> pass;
    if(pass.length() < 6)
    {
        cout << "Password length must be atleast 6 characters long";
    }
    else 
    {
        if(usr == USERNAME && pass == PASSWORD)
        {
            cout << "nnSuccessfully granted access" << endl;
        }
        else
        {
            cout << "Invalid login details" << endl;
        }
    }
}

这就是我的代码的样子。我想做的是,当我输入错误的用户名或错误的密码时,程序会显示我写的消息,然后让我引入另一个用户名和密码,当我正确引入它们时,程序启动。

我会创建一个logged_in变量,然后在条件传递时将其设置为 true 并在 while 循环中运行整个登录过程:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    const string USERNAME = "claudiu";
    const string PASSWORD = "123456";
    string usr, pass;
    bool logged_in = false;
    while (!logged_in)
    {
        cout << "Enter Username : ";
        cin >> usr;
        if (usr.length() < 4)
        {
            cout << "Username length must be atleast 4 characters long.";
        }
        else
        {
            cout << "Enter Password : ";
            cin >> pass;
            if (pass.length() < 6)
            {
                cout << "Password length must be atleast 6 characters long";
            }
            else
            {
                if (usr == USERNAME && pass == PASSWORD)
                {
                    cout << "nnSuccessfully granted access" << endl;
                    logged_in = true;
                }
                else
                {
                    cout << "Invalid login details" << endl;
                }
            }
        }
    }
    cout << "Passed login!n";
}