Question

In: Computer Science

c++ Develop a program that validates a password entered by a user. The password must meet...

c++

Develop a program that validates a password entered by a user. The password must meet the following criteria:

  1. Be at least six characters long.
  2. Contain at least one uppercase and at least one lowercase letter.
  3. Have at least one digit.

Write a program that asks for a password and then verifies that it meets the stated criteria. If it does not, the program should display a message telling the user why and ask for another password. The program should keep asking for the password until the password meets the criteria.

The validation should be done on a separate function called validation, which takes a string variable as parameter and returns an integer. It should return 0 if the password is valid and 1 if it does not match the first criteria, 2 if it does not match the second criteria and 3 if it does not match the third criteria.

Solutions

Expert Solution

Code:

#include <iostream>
//as we are using isupper() ,islower(),and isdigit()
//functions, we have to include this library
#include <cctype>
using namespace std;
//function named validation which takes a string named
//pass as argument and returns a int is defined
int validation(string pass){
   //length of pass
   int len=pass.length();
   //variables to store counts of uppercase,
   //lower case, and digits
   int upper_count=0;
   int lower_count=0;
   int digit_count=0;
   //traversing the string and checking each character
   //and incrementing the respective count variable
   for(int i=0;i<len;i++){
       if(isupper(pass[i])){
           upper_count++;
       }
       if(islower(pass[i])){
           lower_count++;
       }
       if(isdigit(pass[i])){
           digit_count++;
       }
   }
   //if len is lessthan 6 returning 1
   if(len<6){
       return 1;
   }
   //if either of uppercount and lowercount are lessthan 1
   //returning 2
   else if(upper_count<1 || lower_count<1){
       return 2;
   }
   //if digit_count is lessthan 1 returning 3
   else if(digit_count<1){
       return 3;
   }
   //if all of the above cases are false then return 0
   else{
       return 0;
   }
}
//main functions
int main(){
   //a string named password
   string password;
   //this loop iterates until a valid password is entered
   while(true){  
   //taking input
   cout<<"Enter password: "<<endl;
   cin>>password;
   //calling function validation with argument password
   //and storing the return value in res
   int res = validation(password);
   //showing the reason
   if(res==0){
       cout<<"password is valid"<<endl;
       break;
      
   }
   else if(res==1){
       cout<<"Password should be atleast six characters"<<endl;
   }
   else if(res==2){
       cout<<"Password should contain atleast 1 uppercase and 1 lowercase letters"<<endl;
   }
   else if(res==3){
       cout<<"Passsword should contain atleast 1 digit"<<endl;
   }
   else{
       cout<<"Invalid"<<endl;
   }
}
                  
}

Output:

Code Screenshot:

Code Snippet:

#include <iostream>
//as we are using isupper() ,islower(),and isdigit()
//functions, we have to include this library
#include <cctype>
using namespace std;
//function named validation which takes a string named
//pass as argument and returns a int is defined
int validation(string pass){
        //length of pass
        int len=pass.length();
        //variables to store counts of uppercase,
        //lower case, and digits
        int upper_count=0;
        int lower_count=0;
        int digit_count=0;
        //traversing the string and checking each character
        //and incrementing the respective count variable
        for(int i=0;i<len;i++){
                if(isupper(pass[i])){
                        upper_count++;
                }
                if(islower(pass[i])){
                        lower_count++;
                }
                if(isdigit(pass[i])){
                        digit_count++;
                }
        }
        //if len is lessthan 6 returning 1
        if(len<6){
                return 1;
        }
        //if either of uppercount and lowercount are lessthan 1
        //returning 2
        else if(upper_count<1 || lower_count<1){
                return 2;
        }
        //if digit_count is lessthan 1 returning 3
        else if(digit_count<1){
                return 3;
        }
        //if all of the above cases are false then return 0
        else{
                return 0;
        }
}
//main functions
int main(){
        //a string named password
        string password;
        //this loop iterates until a valid password is entered
        while(true){    
        //taking input
        cout<<"Enter password: "<<endl;
        cin>>password;
        //calling function validation with argument password
        //and storing the return value in res
        int res = validation(password);
        //showing the reason
        if(res==0){
                cout<<"password is valid"<<endl;
                break;  
        }
        else if(res==1){
                cout<<"Password should be atleast six characters"<<endl;
        }
        else if(res==2){
                cout<<"Password should contain atleast 1 uppercase and 1 lowercase letters"<<endl;
        }
        else if(res==3){
                cout<<"Passsword should contain atleast 1 digit"<<endl;
        }
        else{
                cout<<"Invalid"<<endl;
        }
}
                                        
}

Related Solutions

C# Create an application that asks the user to enter their new password. The password must...
C# Create an application that asks the user to enter their new password. The password must be at least 6 characters long. Develop a custom exception handling case that checks for the password length. (hint: use " .Length " built-in method). If the new password is less than 6 characters long the custom exception should output an error message.
Using C++ Write a program to ask user to enter a password and validity the password....
Using C++ Write a program to ask user to enter a password and validity the password. The password should be 8 – 20 characters long (8 is included and 20 is included), it should contain at least one uppercase letter and one lower case letter. The password should contain at least one digit, and at least one symbol other than alphabets or numbers. Most importantly, the password may not contain any white space. Functions: You will need at least the...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user...
Write a program which: Prompts the user for a positive integer >= 0 Validates the user input to ensure it is a positive integer >= 0 Allocate (dynamically) an array big enough for the data. Load the array with random numbers ranging in value from1 to 100 Display the elements of the array (unsorted) Display the elements of the array (sorted) Display the average Display the median Display the mode, if none, display appropriate message RESTRICTIONS No global variables No...
Write a program that validates passwords based on the following requirements: • must be at least...
Write a program that validates passwords based on the following requirements: • must be at least 8 characters in length • must include at least 1 alphabet character • must include at least 1 number The program should implement the password checker using a function name validate_password, which takes two strings as input and returns one of the following: • 0 if the passwords match and fulfill all requirements • 1 if the passwords are not the same length •...
Write a program that validates passwords based on the following requirements: • must be at least...
Write a program that validates passwords based on the following requirements: • must be at least 8 characters in length • must include at least 1 alphabet character • must include at least 1 number The program should implement the password checker using a function name validate_password, which takes two strings as input and returns one of the following: • 0 if the passwords match and fulfill all requirements • 1 if the passwords are not the same length •...
C++ program that will ask the user for how many test scores will be entered. Setup...
C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. The data will include the student’s first name and midterm score Print out the completed test scores to a file (midTermScores.txt) . Print out list of students names and grades in the print out, a letter grade should replace numeric score using standard grading (a = 90 – 100, b=80-90, c=70-80, d=60-70, f=below 60)
Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
2. Create a C++ program that converts the number of American dollars entered by the user...
2. Create a C++ program that converts the number of American dollars entered by the user into one of the following foreign currencies: Euro, British pound, German mark, or Swiss franc. Allow the user to select the foreign currency from a menu. Store the exchange rates in a four-element double array named rates. Notice that the menu choice is always one number more than the subscript of its corresponding rate. For example, menu choice 1's rate is stored in the...
Class Instructions You will create a password verification program using C-strings and character testing. The user...
Class Instructions You will create a password verification program using C-strings and character testing. The user will enter a password as a C-string. (Character array). You must test that the password contains at least: One lowercase letter One uppercase letter One number (0-9) The password can contain no spaces You are to add one more requirement to the password. I need help in setting this program up. #include <iostream> #include <cctype> using namespace std; int main() { char input; cout...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT