密码验证c ++ [英] Password Authentication c++

查看:109
本文介绍了密码验证c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,这是我第一次使用类,因此我的可怜解释道歉。基本上我正在为电梯程序制作密码功能。 LogIn是我的类的名称,它包含字符串john,这是密码。一切似乎工作正常,除了循环不正确的密码尝试。

Hi this is my first time using classes so apologies for my poor explanation. Basically I am making a password function for an elevator program. LogIn is the name of my class, which contains the string "john" which is the password. Everything seems to be working fine except the loop for incorrect password attempts.

如果密码尝试第一次正确,则代码工作正常,但如果输入的密码不正确,则行名称不正确。再试一次出现用于接下来的两次尝试,不管是否正确输入了密码。我希望有人能看到我会错了。 name 是存储的密码, nameAttempt 是用户输入的尝试密码。

If the password attempt is correct the first time then the code workds fine, however if a password is entered incorrectly then the line "Incorrect name. Try again" appears for the next two attempts, regardless of whether or not the password has been entered correctly. I was hoping someone could see where I'm going wrong. name is the stored password and nameAttempt is the attempted password inputted bu the user.

#include "stdafx.h"
#include "LogIn.h"
#include <iostream>
#include <iostream>
#include <string>

using namespace std;


bool password() {

    string name;
    string nameAttempt; 
    int attempts = 0;   

    cout << "nameAttempt: " << endl;
    cin >> nameAttempt; 

    LogIn Authenticate(name, nameAttempt);


    if (Authenticate.getName() == Authenticate.getNameAttempt()) 
    {
            return true;
    }
    else
            while (Authenticate.getName() != Authenticate.getNameAttempt())
            {
                    if (attempts++ ==2)
                    {
                            return false;
                    }       
                    cout<<"Incorrect name. Try again"<< endl;
                    cout<< "" << endl;

                    cout << "Enter Name:"<< endl;
                    cin >>nameAttempt;
            }
}



int main()
{

    bool password();

    bool loggedin = password();

    if(loggedin) {
        cout << "Password Correct" << endl;
    }

    if(!loggedin) {
        cout << "Incorrect Password" << endl;
        cout << "Program will now terminate" << endl;
        system("pause");
        return 0;   
    }

    cout << "you are now free to enter lift" << endl;

    system("pause");
    return 0;
}


推荐答案

cout << "nameAttempt: " << endl;
cin >> nameAttempt; 

LogIn Authenticate(name, nameAttempt);
attempts = 0;
while (attempts<2)
{
    if (Authenticate.getName() == Authenticate.getNameAttempt()) 
    {
      return true;
     }

     cout<<"Incorrect name. Try again"<< endl;
     cout<< "" << endl;

     cout << "Enter Name:"<< endl;
     cin >>nameAttempt;
     attempts++;
     LogIn Authenticate(name, nameAttempt);
 }
return false;

这篇关于密码验证c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆