//Ryan Shahriyarpour CS1A Chapter 10 Question 12
/*****************************************************************************
*
* Password Verifier
*
*********************
*
* This program asks the user for a password and verifies that it meets
* the following criteria:
* - at least six characters long
* - at least one uppercase letter
* - at least one lowercase letter
* - at least one digit
* If the password fails any criteria, a message is displayed for each
* one that was not met.
*
* INPUT
* password - the password entered by the user
*
* OUTPUT
* A message saying the password is valid, or why it is not
*
****************************************************************************/
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
// Constants
const int SIZE = 50; // size of the password array
const int MIN_LENGTH = 6; // minimum password length
// Function prototype
bool verifyPassword(const char *pass);
int main()
{
// DATA DICTIONARY
// SIZE - size of the password array (constant)
// MIN_LENGTH - minimum number of characters required (constant)
// password - the password entered by the user
char password[SIZE];
// Get the password
cout << "Enter a password: ";
cin.getline(password, SIZE);
// Verify the password and report the result
if (verifyPassword(password))
cout << "\nYour password is valid." << endl;
else
cout << "\nPlease try again with a password that meets "
<< "all of the criteria." << endl;
return 0;
}
// Checks the password against each criterion. Displays a message for every
// criterion that fails. Returns true only if all of them are met.
bool verifyPassword(const char *pass)
{
// DATA DICTIONARY
// hasUpper - true if an uppercase letter was found
// hasLower - true if a lowercase letter was found
// hasDigit - true if a digit was found
// valid - true if every criterion is met
// index - loop counter
bool hasUpper = false;
bool hasLower = false;
bool hasDigit = false;
bool valid = true;
int index;
// Check the length
if (strlen(pass) < MIN_LENGTH)
{
cout << "\n- The password must be at least " << MIN_LENGTH
<< " characters long." << endl;
valid = false;
}
// Scan each character
for (index = 0; *(pass + index) != '\0'; index++)
{
if (isupper(*(pass + index)))
hasUpper = true;
else if (islower(*(pass + index)))
hasLower = true;
else if (isdigit(*(pass + index)))
hasDigit = true;
}
// Report anything that was missing
if (!hasUpper)
{
cout << "- The password must contain at least one "
<< "uppercase letter." << endl;
valid = false;
}
if (!hasLower)
{
cout << "- The password must contain at least one "
<< "lowercase letter." << endl;
valid = false;
}
if (!hasDigit)
{
cout << "- The password must contain at least one digit." << endl;
valid = false;
}
return valid;
}