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. int main()
  31. {
  32. // DATA DICTIONARY
  33. // SIZE - size of the password array (constant)
  34. // MIN_LENGTH - minimum number of characters required (constant)
  35. // password - the password entered by the user
  36. // hasUpper - true if an uppercase letter was found
  37. // hasLower - true if a lowercase letter was found
  38. // hasDigit - true if a digit was found
  39. // valid - true if every criterion is met
  40. // index - loop counter
  41. const int SIZE = 50;
  42. const int MIN_LENGTH = 6;
  43. char password[SIZE];
  44. bool hasUpper = false;
  45. bool hasLower = false;
  46. bool hasDigit = false;
  47. bool valid = true;
  48. int index = 0;
  49.  
  50. // Get the password
  51. cout << "Enter a password: ";
  52. cin.getline(password, SIZE);
  53.  
  54. // Check the length
  55. if (strlen(password) < MIN_LENGTH)
  56. {
  57. cout << "\n- The password must be at least " << MIN_LENGTH
  58. << " characters long." << endl;
  59. valid = false;
  60. }
  61.  
  62. // Scan each character
  63. for (index = 0; *(password + index) != '\0'; index++)
  64. {
  65. if (isupper(*(password + index)))
  66. hasUpper = true;
  67. else if (islower(*(password + index)))
  68. hasLower = true;
  69. else if (isdigit(*(password + index)))
  70. hasDigit = true;
  71. }
  72.  
  73. // Report anything that was missing
  74. if (!hasUpper)
  75. {
  76. cout << "- The password must contain at least one "
  77. << "uppercase letter." << endl;
  78. valid = false;
  79. }
  80. if (!hasLower)
  81. {
  82. cout << "- The password must contain at least one "
  83. << "lowercase letter." << endl;
  84. valid = false;
  85. }
  86. if (!hasDigit)
  87. {
  88. cout << "- The password must contain at least one digit." << endl;
  89. valid = false;
  90. }
  91.  
  92. // Display the final result
  93. if (valid)
  94. cout << "\nYour password is valid." << endl;
  95. else
  96. cout << "\nPlease try again with a password that meets "
  97. << "all of the criteria." << endl;
  98.  
  99. return 0;
  100. }
Success #stdin #stdout 0s 544KB
stdin
AaBb12
stdout
Enter a password: 
Your password is valid.