Thursday, April 27, 2017

Programming Challenge 10.12 - Password Verifier

/* Password Verifier - Imagine we are developing a software package that
    requires users to enter their own passwords. Our software requires that
    user's passwords meet the following criteria:
  
        * The password should be at least six characters long
        * The password should contain at least one uppercase
          and at least one lowercase letter
        * The password should have at least one digit

    This program asks for a password and then verifies that it meets the
    stated criteria. If it doesn't, the program displays a message telling
    the user why. */

#include "Utility.h"

/* Determines the length of password,
    returns the result */
bool checkLength(string);

/* Determines whether the password contains uppercase characters,
    returns the result */
bool containsUpper(string);

/* Determines whether the password contains lowercase characters,
    returns the result */
bool containsLower(string);

/* Determines whether the password contains digits,
    returns the result */
bool containsDigit(string);

int main()
{  
    int    numTries = 5;
    string passphrase = "moRioka9713";
    string password = " ";

    cout << "\n\tKaibun Game - Login\n"
          << "\t-------------------\n";

    do
    {
        cout << "\n\tPlease enter your password: ";
        getline(cin, password);

        if (password != passphrase)
        {
            if (checkLength(password) == false)
            {
                cout << "\n\tPassword length mismatch.\n";
            }

            if (containsUpper(password) == false)
            {
                cout << "\tPassword does not contain uppercase characters.\n";
            }

            if (containsLower(password) == false)
            {
                cout << "\tPassword does not contain lowercase characters.\n";
            }

            if (containsDigit(password) == false)
            {
                cout << "\tPassword does not contain digits.\n";
            }

            cout << "\n\tYou have " << (numTries -= 1) << " tries left\n";

            if (numTries == 0)
            {
                cout << "\n\tYour password could not be verified.\n"
                      << "\tNow closing this program ...";
            }
        }

        else if (password == passphrase)
        {
            cout << "\n\tYou have been verified successfully.\n";
        }
    } while (numTries > 0 && password != passphrase);

    pauseSystem();
    return 0;
}

/* **********************************************************
    Definition: checkLength

    This function verifies that the password is at least six
    characters long. The result is returned.
    ********************************************************** */

bool checkLength(string password)
{
    bool length = false;

    password.length() >= 6 ? length = true : length = false;

    return length;
}

/* **********************************************************
    Definition: containsUpper

    This function verifies that the password contains at least
    one uppercase character. The result is returned.
    ********************************************************** */

bool containsUpper(string password)
{
    bool isUpper = false;

    for (size_t i = 0; i < password.length(); i++)
    {
        if (isupper(password[i]))
        {
            isUpper = true;
        }
    }

    return isUpper;
}

/* **********************************************************
    Definition: containsLower

    This function verifies that the password contains at least
    one lowercase character. The result is returned.
    ********************************************************** */

bool containsLower(string password)
{
    bool isLower = false;

    for (size_t i = 0; i < password.length(); i++)
    {
        if (islower(password[i]))
        {
            isLower = true;
        }
    }

    return isLower;
}

/* **********************************************************
    Definition: containsDigit

    This function determines whether the password contains any
    digits. The result is returned.
    ********************************************************** */

bool containsDigit(string password)
{
    bool isDigit = false;

    for (size_t i = 0; i < password.length(); i++)
    {
        if (isdigit(password[i]))
        {
            isDigit = true;
        }
    }

    return isDigit;
}

Example Output:




No comments:

Post a Comment