Write a program in C++ that validates a password according to the following rule
ID: 3673874 • Letter: W
Question
Write a program in C++ that validates a password according to the following rules:
-The password must be at least 8 characters long
-It must contain at least one upper-case letter, one lower-case letter, and one digit.
-It must contain at least one of the following special characters: ~!@#$%^&*()_+=-<>{}[],. (yes, that includes the comma and the period.)
-It must not contain spaces.
The program should loop, requesting user input, until a valid password is entered. For each input, print a message that says the input is either valid or invalid. It should use the “string’ data type, not c-string, and the various functions of that type. You can’t use “cin” for this, so you’ll have to use getline.
Explanation / Answer
# include <iostream>
# include <string>
using namespace std;
class Password{
private:
static const int SIZE = 80;
unsigned short password[SIZE]; // used to encrypt password.
//string password1;
public:
// constructor one takes an array of characters as an argument.
Password(char *charArray){
for (int i = 0; charArray[i] != '' ; i++){
password[i] = charArray[i] <<1; // bit shift
}
}
// constructor2 takes a string as an argument.
Password(string charArray){
for (int i = 0; charArray[i] != '' ; i++){
password[i] = charArray[i] <<1; // bit shift
}
}
// prints password.
void printPassword(){
for (int i = 0; password[i] != ''; i++){
cout << password[i];
}
}
};
// Used to measure the string lenght.
int stringLength(char *cArray){
int length = 0;
for (int i = 0; cArray[i] != ''; i++){
length++;
}
return length;
}
// Overloaded function takes a array of characters.
bool validatePass(char *cArray){
bool upper = false;
bool lower = false;
bool digit = false;
bool special = false;
for(int i =0; cArray[i] != ''; i++){ // Loop through each character of the string and compare requirements.
if (ispunct(cArray[i])){ // If you find a special character.
special = true;
}
if(isupper(cArray[i])){ // if you find an upper case leeter.
upper = true;
}
if(islower(cArray[i])){ // If you find a lower case letter.
lower = true;
}
if(isdigit(cArray[i])){ // if you find a number.
digit = true;
}
}
if(!upper){ // if no upper case letter; tell the user.
cout << "Password requires a uppercase letter!" << endl;
}
if(!lower){ // if no lower case letter; tell the user.
cout << "Password requires a lowercase letter!" << endl;
}
if(!digit){ // if no number; tell the user.
cout << "Password requires a number!" << endl;
}
if(!special){ // if no special character; tell the user.
cout << "Password requires a special character!" << endl;
}
if(stringLength(cArray) < 8){ // if not long enough; tell the user.
cout << "Password must be at least 8 characters long!" << endl;
}
if (upper && lower && digit && special && (stringLength(cArray) > 8)){ // if password fulfills the requirement; tell the user and return true.
cout << "password meets all requirements" << endl;
return true;
}
else{ // return false if the password is invalid.
return false;
}
}
// Overloaded functions takes a string.
bool validatePass(string cArray){
bool upper = false;
bool lower = false;
bool digit = false;
bool special = false;
for(int i =0; cArray[i] != ''; i++){ // Loop through each character of the string and compare requirements.
if (ispunct(cArray[i])){
special = true;
}
if(isupper(cArray[i])){
upper = true;
}
if(islower(cArray[i])){
lower = true;
}
if(isdigit(cArray[i])){
digit = true;
}
}
if(!upper){ // if no upper case letter; tell the user.
cout << "Password requires a uppercase letter!" << endl;
}
if(!lower){ // if no lower case letter; tell the user.
cout << "Password requires a lowercase letter!" << endl;
}
if(!digit){ // if no number; tell the user.
cout << "Password requires a number!" << endl;
}
if(!special){ // if no special character; tell the user.
cout << "Password requires a special character!" << endl;
}
if(cArray.size() < 8){ // if not long enough; tell the user.
cout << "Password must be at least 8 characters long!" << endl;
}
if (upper && lower && digit && special && (cArray.size() > 8)){ // if password fulfills the requirement; tell the user and return true.
cout << "password meets all requirements" << endl;
return true;
}
else{ // return false if the password is invalid.
return false;
}
}
int main(){
const int LENGTH = 80;
char line[LENGTH]; // array of characters.
char choice = ' ';
do{
cout << "Testing password checker. What data type should the password be? c) c-string, s) string class, q) quit:";
cin >> choice;
switch (choice){
case 'c': {
bool valPass = false; // valpass equals false until the password is valid.
while (!valPass){ // Loop until the password is valid.
cout << "Enter your password:(0 to Menu)";
cin >> line;
if (line[0] == '0'){
break;
}
valPass = validatePass(line); // function to determine if password is valid.
}
if (valPass){
Password ob(line);
}
//cout << "Password valid." << endl;
break;
}
case 's': {
bool valPass2 = false; // valpass2 equals false until the password is valid.
string password = " ";
while (!valPass2){ // Loop until password is valid(true).
cout << "Enter your string password:(0 to Menu)";
cin >> password;
if (password[0] == '0'){
break;
}
valPass2 = validatePass(password); // function to determine if password is valid.
}
if(valPass2){
Password ob2(password);
}
break;
}
case 'q': {
cout << "Thank You goodBye." << endl;
break;
}
default:{
cout << "Invalid Choice!" << endl;
}
}
}while (choice != 'q');
return 0;
}
output
Testing password checker. What data type should the password be? c) c-string, s)
string class, q) quit:c
Enter your password:(0 to Menu)d
Password requires a uppercase letter!
Password requires a number!
Password requires a special character!
Password must be at least 8 characters long!
Enter your password:(0 to Menu)Dj1234&^%
password meets all requirements
Testing password checker. What data type should the password be? c) c-string, s)
string class, q) quit:s
Enter your string password:(0 to Menu)d
Password requires a uppercase letter!
Password requires a number!
Password requires a special character!
Password must be at least 8 characters long!
Enter your string password:(0 to Menu)Dj1234&^%
password meets all requirements
Testing password checker. What data type should the password be? c) c-string, s)
string class, q) quit:q
Thank You goodBye.
--------------------------------
Process exited with return value 0
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.