fork download
  1. //Ryan Shahriyarpour CS1A Chapter 10 Question 12
  2. /*****************************************************************************
  3.  *
  4.  * Password Verifier
  5.  *
  6.  *********************
  7.  *
  8.  * This program asks the user for a password and verifies that it meets
  9.  * the following criteria:
  10.  * - at least six characters long
  11.  * - at least one uppercase letter
  12.  * - at least one lowercase letter
  13.  * - at least one digit
  14.  * If the password fails any criteria, a message is displayed for each
  15.  * one that was not met.
  16.  *
  17.  * INPUT
  18.  * password - the password entered by the user
  19.  *
  20.  * OUTPUT
  21.  * A message saying the password is valid, or why it is not
  22.  *
  23.  ****************************************************************************/
  24.  
  25. #include <iostream>
  26. #include <cstring>
  27. #include <cctype>
  28. using namespace std;
  29.  
  30. // Constants
  31. const int SIZE = 50; // size of the password array
  32. const int MIN_LENGTH = 6; // minimum password length
  33.  
  34. // Function prototype
  35. bool verifyPassword(const char *pass);
  36.  
  37. int main()
  38. {
  39. // DATA DICTIONARY
  40. // SIZE - size of the password array (constant)
  41. // MIN_LENGTH - minimum number of characters required (constant)
  42. // password - the password entered by the user
  43. char password[SIZE];
  44.  
  45. // Get the password
  46. cout << "Enter a password: ";
  47. cin.getline(password, SIZE);
  48.  
  49. // Verify the password and report the result
  50. if (verifyPassword(password))
  51. cout << "\nYour password is valid." << endl;
  52. else
  53. cout << "\nPlease try again with a password that meets "
  54. << "all of the criteria." << endl;
  55.  
  56. return 0;
  57. }
  58.  
  59. // Checks the password against each criterion. Displays a message for every
  60. // criterion that fails. Returns true only if all of them are met.
  61. bool verifyPassword(const char *pass)
  62. {
  63. // DATA DICTIONARY
  64. // hasUpper - true if an uppercase letter was found
  65. // hasLower - true if a lowercase letter was found
  66. // hasDigit - true if a digit was found
  67. // valid - true if every criterion is met
  68. // index - loop counter
  69. bool hasUpper = false;
  70. bool hasLower = false;
  71. bool hasDigit = false;
  72. bool valid = true;
  73. int index;
  74.  
  75. // Check the length
  76. if (strlen(pass) < MIN_LENGTH)
  77. {
  78. cout << "\n- The password must be at least " << MIN_LENGTH
  79. << " characters long." << endl;
  80. valid = false;
  81. }
  82.  
  83. // Scan each character
  84. for (index = 0; *(pass + index) != '\0'; index++)
  85. {
  86. if (isupper(*(pass + index)))
  87. hasUpper = true;
  88. else if (islower(*(pass + index)))
  89. hasLower = true;
  90. else if (isdigit(*(pass + index)))
  91. hasDigit = true;
  92. }
  93.  
  94. // Report anything that was missing
  95. if (!hasUpper)
  96. {
  97. cout << "- The password must contain at least one "
  98. << "uppercase letter." << endl;
  99. valid = false;
  100. }
  101. if (!hasLower)
  102. {
  103. cout << "- The password must contain at least one "
  104. << "lowercase letter." << endl;
  105. valid = false;
  106. }
  107. if (!hasDigit)
  108. {
  109. cout << "- The password must contain at least one digit." << endl;
  110. valid = false;
  111. }
  112.  
  113. return valid;
  114. }
Success #stdin #stdout 0s 548KB
stdin
AaBb123
stdout
Enter a password: 
Your password is valid.