C++ c++;验证登录代码

C++ c++;验证登录代码,c++,validation,login,C++,Validation,Login,我正在尝试为我的程序创建一个简单的验证用户名和密码。 我不确定我的代码出了什么问题,但我认为我有一些逻辑错误 这是我的一段代码 string usernameinput,passinput; string username[]={"mmm","nnnn","rrrr","aaa"}; string password[]={"1234","1212","1234","1212"}; bool flag=false; while(flag==false){ cout <

我正在尝试为我的程序创建一个简单的验证用户名和密码。 我不确定我的代码出了什么问题,但我认为我有一些逻辑错误

这是我的一段代码

          string usernameinput,passinput;
string username[]={"mmm","nnnn","rrrr","aaa"};
string password[]={"1234","1212","1234","1212"};
bool flag=false;

while(flag==false){
  cout << "username: "<<endl;
    cin>>usernameinput;
    cout << "password: "<<endl;
        cin>>passinput;
        for(int i=0;i<=3;i++){
            if(usernameinput ==username[i] && passinput==password[i] )
            {
                flag=true;
            }
            else if (usernameinput !=username[i] && passinput!=password[i])
            {
                flag=false;
    cout<<"please enter correct username and password"<<endl;
            }

        }
}
stringusernameinput,passinput;
字符串用户名[]={“mmm”、“nnnn”、“rrrrr”、“aaa”};
字符串密码[]={“1234”、“1212”、“1234”、“1212”};
布尔标志=假;
while(flag==false){

cout您不需要第二个
if
语句,
else
就足够了。即使您保留它,它也必须是一个
|
而不是
&
,因为任何一个不匹配都会将标志设置为false

事实上,您甚至不需要
else
语句。因为您正在将标志初始化为
false
,所以它将保持
false
,直到它被设置为
true
,这只会在用户名和密码都匹配时发生。一旦存在匹配,您就要停止比较,因此您必须使用
break
来结束
for
循环。错误消息应该在
for
循环之外,因为在对照所有值进行检查之前,您不希望它显示出来

bool flag=false;

while(flag==false){
    cout << "username: "<<endl;
    cin>>usernameinput;
    cout << "password: "<<endl;
    cin>>passinput;
    for(int i=0;i<=3;i++){
        if(usernameinput ==username[i] && passinput==password[i] )
        {
            flag=true;
            break;
        }
    }
    if(!$flag) {
        cout<<"please enter correct username and password"<<endl;
    }
}
bool标志=false;
while(flag==false){
库特问题
在找到匹配项后,程序不会停止搜索匹配项。由于匹配项后测试的值将不是匹配项,因此会出现混乱

让我们看看当我们分别为输入mmm和用户名和密码执行以下代码时会发生什么:

for(int i=0;i<=3;i++){
    if(usernameinput ==username[i] && passinput==password[i] )
    {
        flag=true;
    }
    else if (usernameinput !=username[i] && passinput!=password[i])
    {
        flag=false;
        cout<<"please enter correct username and password"<<endl;
    }
}
我们需要在匹配时退出循环。显而易见的解决方案是:

    if(usernameinput ==username[i] && passinput==password[i] )
    {
        flag=true;
        break;
    }
但是等等!还有更多!如果输入是mmm和1235呢

迭代1:

    if("mmm" == "mmm"  && "1234" == "1234" ) // both true. Enter
    {
        flag=true; // flag is now true
    }
    else if ("mmm" != "mmm"  && "1234" != "1234")
    {
        flag=false;
        cout<<"please enter correct username and password"<<endl;
    }
    if("mmm" == "mmm"  && "1235" == "1234" ) //Second case fails
    {
        flag=true; 
        break;
    }
    else if ("mmm" != "mmm"  && "1234" != "1234") // first case fails
    {
        flag=false;
        cout<<"please enter correct username and password"<<endl;
    }
调用函数的作用如下

if (checkcredentials(usernameinput, passinput))
{
    // let the user in.
}
else
{
    cout<<"please enter correct username and password"<<endl;
}

离题:与其将密码和用户名分离到各自的数组中,不如使用配对结构将它们分组,并且只有一个数组。所需的簿记更少。
    if("mmm" == "mmm"  && "1235" == "1234" ) //Second case fails
    {
        flag=true; 
        break;
    }
    else if ("mmm" != "mmm"  && "1234" != "1234") // first case fails
    {
        flag=false;
        cout<<"please enter correct username and password"<<endl;
    }
bool checkcredentials(const std::string & uname,
                     const std::string & pword)
{
    bool rval = false;
    for(int i=0;i<=3;i++)
    {
        if(uname== username[i])
        {
            if (pword==password[i])
            {
                rval = true; // could just return true here, but some folk get uptight 
                             // over multiple returns in a function
            }
            break;
        }
    }
    return rval;
}
if (checkcredentials(usernameinput, passinput))
{
    // let the user in.
}
else
{
    cout<<"please enter correct username and password"<<endl;
}
pair<string, string> credentials[]={ // array of pairs
    {"mmm",  "1234"},
    {"nnnn", "1212"},
    {"rrrr", "1234"},
    {"aaa",  "1212"}
 };

bool checkcredentials(const std::string & uname,
                      const std::string & pword)
{
    bool rval = false;
    for(auto & cred: credentials)
    {
        if(uname == cred.first)
        {
            if (pword == cred.second)
            {
                rval = true;
            }
            break;
        }
    }
    return rval;
}
map<string, string> credentials={
    {"mmm",  "1234"},
    {"nnnn", "1212"},
    {"rrrr", "1234"},
    {"aaa",  "1212"}
 };

bool checkcredentials(const std::string & uname,
                      const std::string & pword)
{
    auto cred = credentials.find(uname); // look for user name
    if (cred != credentials.end() && // username exists
            cred->second == pword) // password matches
    {
        return true;
    }
    return false;
}