Write a program that requires a user to enter their own password as part of crea
ID: 3741606 • Letter: W
Question
Write a program that requires a user to enter their own password as part of creating their online account. Your program will require the user's passwords meet the following criteria 1) The password should be at least eight (8) characters long. 2) The password should contain at least one (1) uppercase letter and at least one (1) lowercase letter. 3) The password should have at least one (1) number 4) The password must start with a letter. Your job is to write a program that asks the users for a password and then calls a function to verify if the password meets the criteria specified If the password does not meet the criteria, tell the user why if failed validation. (HINT: Have your function return a boolean result and pass a second parameter by reference that contains the error message) Upload you Main.cpp file into Blackboard when completed.Explanation / Answer
Here is the required method that you asked. It will validate a password according to the specified norms and conditions. I have not used any built in methods to check if a character is letter, upper case , lower case or numeric. Comments are included , check the code and drop a comment if you have any queries. Thanks.
//code.cpp
#include<iostream>
using namespace std;
/* method to check if a password is valid or not, also appends the result in message variable */
bool isValid(string password, string &message){
//defining all required variables to keep the count of different attributes
int length=0,upper_count=0,lower_count=0,num_count=0;
//finding the length
length=password.length();
if(length<8){
//invalid length
message="Password should be atleast 8 characters long.";
return false;
}
//validating everything without using any built in functions
if((password[0]>='A' && password[0]<='Z') || (password[0]>='a' && password[0]<='z')){
//starting with a letter
/*looping through all characters*/
for(int i=0;i<length;i++){
if(password[i]>='A' && password[i]<='Z'){
//character is between A-Z, upper case
upper_count++;
}
if(password[i]>='a' && password[i]<='z'){
//character is between a-z, lower case
lower_count++;
}
if(password[i]>='0' && password[i]<='9'){
//character is between 0-9 (char equivalent)
num_count++;
}
}
//checking stats
if(upper_count==0){
message="Password should contain atleast 1 upper case character";
return false;
}
if(lower_count==0){
message="Password should contain atleast 1 lower case character";
return false;
}
if(num_count==0){
message="Password should contain atleast 1 number";
return false;
}
//all tests have passed, password is valid
return true;
}else{
message="Password should start with a letter.";
return false;
}
}
int main(){
//prompting user to enter a password and verify it
string msg,password;
cout<<"Enter a password: ";
cin>>password;
if(isValid(password,msg)){
cout<<"Password is valid!"<<endl;
}else{
cout<<"Password is not valid! "<<msg<<endl;
}
return 0;
}
//OUTPUT
Enter a password: abc
Password is not valid! Password should be atleast 8 characters long.
Enter a password: abcdefgh
Password is not valid! Password should contain atleast 1 upper case character
Enter a password: Abcdabcd
Password is not valid! Password should contain atleast 1 number
Enter a password: 1Abcdefgh
Password is not valid! Password should start with a letter.
Enter a password: Abcd1122
Password is valid!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.