Password Authentication c++

2019-03-04 09:28发布

问题:

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.

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;
}

回答1:

In the retry loop, you still need to validate the attempted name and break the loop if the name is accepted.



回答2:

You initialize local function variable

int attempts = 0; 

so exit condition in while loop will be trigerred third times the code

 if (attempts++ ==2)

is run, so you will print two times:

while (Authenticate.getName() != Authenticate.getNameAttempt())
{
    if (attempts++ ==2) // increment attempts
{
    return false;
}      

It looks as it was done deliberately to exit after second print, so your confusion is hard to understand. Use the debugger, this kind of error is very easy to investigate.



回答3:

I think the code should be like this:

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

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

            Authenticate.setNameAttempt(nameAttempt);
        }
    }


回答4:

Try this, sweet and simple:

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;