fork download
  1. // Wyatt Carey CS1A Chapter 10, P. 589, #12
  2. /*******************************************************************************
  3.  * Password Verifier
  4.  * _____________________________________________________________________________
  5.  * This program asks for a password to be created. The password entered must
  6.  * meet the criteria, otherwise it will dislpay an error message. The program
  7.  * use character testing for the password to meet it's criteria.
  8.  * _____________________________________________________________________________
  9.  *
  10.  * INPUTS
  11.  * character testing (cctype functions)
  12.  * OUTPUTS
  13.  * password or error message.
  14.  ******************************************************************************/
  15. #include <iostream>
  16. #include <cctype>
  17. using namespace std;
  18.  
  19. // Function Prototypes.
  20. bool passNum(char [], int);
  21.  
  22. int main()
  23. {
  24. // Array settings.
  25. const int SIZE = 6; // Size of the password.
  26. char password[SIZE]; // To hold the password input.
  27.  
  28. // Ask for the password to be entered.
  29. cout << "\tPassword Criteria\n";
  30. cout << "Enter your password as such: LLlNNN\n";
  31. cout << "L = uppercase, l = lowercase, N = number\n";
  32. cin.getline(password, SIZE);
  33.  
  34. // Password validation
  35. if (passNum(password, SIZE))
  36. cout << "Password accepted.\n";
  37. else
  38. {
  39. cout << "\tINCORRECT PASSWORD\n";
  40. cout << "Enter password as such: LLlNNN";
  41. }
  42. return 0;
  43. }
  44.  
  45. //******************************************************************************
  46. // passNum definition: This function will determine whether testNum parameter *
  47. // holds a valid input password. The size parameter is the size of the passNum *
  48. // array. *
  49. //******************************************************************************
  50.  
  51. bool passNum(char testNum[], int size)
  52. {
  53. int count; // Loop counter.
  54.  
  55. // Test the first two characters of the password.
  56. for (count = 0; count < 2; count++)
  57. {
  58. if (!isupper(testNum[count]))
  59. return false;
  60. }
  61.  
  62. // Test the lowercase letter.
  63. for (count = 0; count <= 1; count++)
  64. {
  65. if (!islower(testNum[count]))
  66. return false;
  67. }
  68.  
  69. // Test the last remaining three digits in the password.
  70. for (count = 0; count < 3; count++)
  71. {
  72. if (!isdigit(testNum[count]))
  73. return false;
  74. }
  75. return true;
  76. }
Success #stdin #stdout 0.01s 5284KB
stdin
JFh387
stdout
	Password Criteria
Enter your password as such: LLlNNN
L = uppercase, l = lowercase, N = number
	INCORRECT PASSWORD
Enter password as such: LLlNNN