lundi 27 avril 2015

Password Checking Program - Matching Password Failure - Looping Failure

As the title states, the program is for users to input a password. The program checks four rules for the password to pass (1). It then prompts the user to enter the password again for validation, which the program then compares to the initial password (2) and states if it the two passwords match and are acceptable.

(1) - The first rule, checking the string length (must be at least 8 characters long), is where the loop appears to state it failed regardless of if it did or not. That said, if it DID actually fail, the program makes the user enter in the password until the user enters in a password that passes this rule.

Example: If I enter in "password", it'll output the prompt that it was entered incorrectly, but will continue with the rest of the program if you enter it correctly a second time.

(2) I can't seem to correctly compare the two strings to check for their accuracy and have tried several techniques to see if it would change. Many ways resulted in the while loop cancelling out entirely.

Here is the code: (I appreciate any and all help that comes my way, been working on this program for a long time now, to no avail)

/*This program determines if the entered password is valid and if the second password entry matches the first.
*/

#include <iostream>

#include <cstring>

#include <string.h>

#include <string>

using namespace std;

void UserInput(string password);
short PasswordAlphaNumCheck(string password);
void PasswordValidationOutput(string password);
void PasswordMatch(string password, string second_password);

int main()
{
    string password;
    string second_password;

    UserInput(password);
    PasswordValidationOutput(password);
    PasswordMatch(password, second_password);

    return 0;
}

void UserInput(string password)
{
    cout << "The Password that you are entering must: " << endl;
    cout << " " << endl;
    cout << "1) Be at least 8 characters long" << endl;
    cout << "2) Contain at least one number" << endl;
    cout << "3) Contain at least one upper case letter" << endl;
    cout << "4) Contain at least one lower case letter" << endl;
    cout << " " << endl;

    cout << "Please enter a password now: " << endl;
    cin >> password;

}

short PasswordAlphaNumCheck(string password)
{
    short flag[3] = {0};

    for(int i = 0; password[i] != '\0'; i++)
    {
        if(isdigit(password[i]))
            flag[0] = 1;
        if(isupper(password[i]))
            flag[1] = 2;
        if(islower(password[i]))
            flag[2] = 4;
    }

   return (flag[0] + flag[1] + flag[2]);

}

void PasswordValidationOutput(string password)
{
    short test = PasswordAlphaNumCheck(password);

    while ((password.length() < 8) && (test < 7))
    {
        while (password.length() < 8)
        {
            cout << " " << endl;
            cout << "Your password is not the correct size." << endl;
            cout << "Please re-enter your password." << endl;
            cin >> password;

        }

        cout << " " << endl;

        test = PasswordAlphaNumCheck(password);

        cout << password
        << " is "
        << (test == 0 ? "not supposed to cause this error. Report to inept computer engineer." :
            test == 1 ? "not valid because you are missing an upper and lower case letter." :
            test == 2 ? "not valid because you are missing an lower case letter and a number." :
            test == 3 ? "not valid because you are missing a lower case letter." :
            test == 4 ? "not valid because you are missing an upper case letter and a number" :
            test == 5 ? "not valid because you are missing an upper case letter." :
            test == 6 ? "not valid because you are missing a number." : "valid") << endl;

        while (test < 7)
        {
            cout << " " << endl;
            cout << "Please re-enter your password." << endl;
            cin >> password;
        }
    }

    cout << " " << endl;
}

void PasswordMatch(string password, string second_password)
{
    cout << "Please enter your password again, for verification" << endl;
    cin >> second_password;

    while (!password.compare(second_password))
    {
        cout << "Your passwords do not match." << endl;
        cout << " " << endl;
        cout << "Please re-enter your password." << endl;
        cin >> second_password;

    }

    cout << "Your password has been approved!" << endl;

}

Aucun commentaire:

Enregistrer un commentaire