Home > Net >  Password validation program cpp
Password validation program cpp

Time:07-17

I am creating a password validation program and I want the password to contain the following:

* Password must be between 8 to 15 characters long...
* Password must contain at least one digit
* Password must contain at least one Uppercase
* Password must contain at least one number
* Password must contain at least one lowercase
* Password must contain at least one symbol 

And if the user enters an invalid password, the program should ask the user to re-enter password. But I'm stuck on how to make the user re-enter the password if its wrong. I tried using 'goto' but its not working...

#include<ctype.h>
#include<stdbool.h>

using namespace std;

bool verify_password();

int main() {
    
    char userName;
    char password[15];

    
    cout << "\n\t\t\t\tEnter User Name: ";
    cin >> userName;
    cin.clear();
    cin.ignore(100, '\n');
    
    
    cout << "\n\t\t\t\tEnter Password: ";
    cin >> password;

    
    bool result = verify_password();
    if (result) cout<<"Verified password\n";
    else {
        cout<<"Invalid password\n";
        cout<<result;
    }
    
        
    system("pause>0");
}

bool verify_password(){
    
    int lenght = strlen(password);
    if (lenght < 8 || lenght > 15) return false;
    
    bool has_upper = false;
    bool has_lower = false;
    bool has_symbol = false;
    bool has_digit = false;
    
    for (int i = 0; i < lenght; i  ){
        
        if(isupper(password[i])) has_upper = true;
        if(islower(password[i])) has_lower = true;
        if(isdigit(password[i])) has_digit = true;
        if(ispunct(password[i])) has_symbol = true;
    }

    if(!(has_upper && has_lower && has_digit && has_symbol)) return false;
    
    
}

CodePudding user response:

I see a lot of issues with this code, but the things you should change to start:

  1. Make your verify password function ONLY verify the password. It is currently doing more than that.
  2. Get user input in the main() function. Once you get the password here, you can do something like:
int trys = 0;
while (trys < 3) {
    cout << "Enter Password: ";
    cin >> password;
    if (verify_password(password)) {
        cout << "valid!" << endl;
        break;
    }

    cout << "invalid..." << endl;
      trys;
}

CodePudding user response:

You can use recursion to execute the function again. Replace the following:

if(!(has_upper && has_lower && has_digit && has_symbol)){ 
       goto checkPassword;
       return false;}

With recursion, such as:

if(!(has_upper && has_lower && has_digit && has_symbol))
       return verify_password();

CodePudding user response:

Using goto statements is not a good practice for coding, it's much clearer to use other kind of loops like while and for.

For your problem here I would suggest to re-organize the code in smaller functions:

requestUsername() //Call it to ask user for the username
requestPassword() //Call it to ask user for password
verifyPassword()  //Call it to verify the provided password

Then, you could structure your main like this:

username = requestUsername();
password = requestPassword();
while(!verifyPassword(password)) //Invalid password
{
  password = requestPassword();
}

Or:

username = requestUsername();
do
{
  password = requestPassword();
}while(!verifyPassword(password)) //Invalid password

Hope this helps!

  • Related