Challenge 14

Description

The purpose of this challenge is to manually manipulate character arrays. This challenge validates a string against password validity rules.

Requirements

      1. Make sure you follow all the steps in order. As you read the instructions below, glance at the sample main() function to get a better understanding of the requirement.
      2. Declare three variables in main: min_uppers, min_lowers, min_length
      3. Write a function int get_int(char prompt[], int min, int max). This function will prompt the user to enter a number that must be between min and max, inclusively. Use this function in main:
        • to ask the user how many upper case numbers are required. Allow the user to enter at least 1 but not more than 3
        • to ask the user how many lower case numbers are required. Allow the user to enter at least 1 but not more than 3
        • to ask the user for the minimum length of the password. Allow the user to enter at least 6 but not more than 8
      4. In the get_int() function, make sure to specify the prompt parameter as a c-string. This parameter represents the text displayed to the user in the function. It is okay to use cout/cin in this function since that is its job.
      5. Write a function int validate_password(char pwd[], int min_uppers, int min_lowers, int min_length). This function will return different integer values to indicate why the validation failed. For example, you could:
        • return a 1 to indicate that the password is too short
        • return a 2 to indicate that the password is too long. Enforce a maximum length of 12 characters.
        • return a 3 to indicate that the password does not have enough upper case characters
        • …and so on
        • return a 0 (zero) to indicate that the password passed in satisfies all the validation requirements.
      6. Then, ask the user to enter sample passwords and use the validate_password() function to validate it against the requirements specified by the user (See sample interaction)

Sample main()

int main()
{
  char password[20];
  int min_uppers, min_lowers, min_length;

  // ask for minimum number of uppers
  min_uppers = get_int("How many upper case characters are required?", 1, 3);

  // ask for minimum number of lowers
  min_lowers = get_int("How many lower case characters are required?", 1, 3);

  // ask for minimum password length
  
  // ask for a password, use cin.getline()

  int val = validate_password(password, min_uppers, min_lowers, min_length);

  while (val != 0)
  {
    switch(val)
    {
      case 1: cout << "password too short\n";
        break;
      case 2: cout << "password too long\n";
        break;

      // more cases here to display messages
      // on the other failed validations
      // see requirement step #5
    }

    // ask for a password again

    // validate it again
    val = validate_password(password, min_uppers, 
min_lowers, min_length);
  }

  cout << "That password is OK\n";

  return 0;
}

Sample Interaction / Output

How many uppers required (1-3)? 2
How many lowers required (1-3)? 1
What is the minimum length (6-8)? 5
: NUMBER MUST BE WITHIN RANGE
What is the minimum length (6-8)? 6

OK
Enter your password: Iamawesome
Password invalid - Needs at least 2 uppers

Enter your password: IAWESOME
Password invalid - Needs at least 1 lower

Enter your password: IRock
Password invalid - too short

Enter your password: IamSimplyAwesome
Password invalid - too long

Enter your password: IamAwesome
That password is OK

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0014

Print Requirements