Question

How do I format this into C++? This the prompt: Design a class named Password that...

How do I format this into C++? This the prompt:

Design a class named Password that stores a password in a c-string and has member functions to test if the password complies with certain requirements as follows:

  • The password should be between 6 and 20 characters long.
  • The password should contain at least one uppercase and at least one lowercase letter.
  • The password should have at least one digit.
  • The password should have at least one punctuation character.

Define a member function to test each one of the requirements. Use the character functions learned in chapter 10 (cctype header file). The constructor should allocate space for the c-strings that stores the password and the destructor should free that memory.

Write a program that asks the user to enter a password, creates an object of the Password class to store the password and tests if it meets the requirements. If it doesn’t, the program should display a message telling the user why and let them try again until the password's requirements can be validated. The main function should ask for a new password until the user enters one that is acceptable.

Turn in a header file (password.h) with the implementation of your class and the main.cpp that tests it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:::

#include <iostream>
#include <string.h>
#include <cctype>
using namespace std;

class Password{
private:
   char * password;
   int length;
public:
   Password(char pass[]){
       int len = strlen(pass);
       password = new char(len+1);
       for(int i=0;i<len;i++){
           password[i] = pass[i];
       }
       length = len;
       password[len] = '\0';
   }

   bool lengthCheck(){
       if(length<6 || length>20){
           return false;
       }
       return true;
   }

   bool checkUpperCase(){
       int count=0;
       for(int i=0;i<length;i++){
           if(isupper(password[i])){
               count++;
           }
       }
       if(count>0){
           return true;
       }
       return false;
   }

   bool checkLowerCase(){
       int count=0;
       for(int i=0;i<length;i++){
           if(islower(password[i])){
               count++;
           }
       }
       if(count>0){
           return true;
       }
       return false;
   }

   bool checkDigit(){
       int count=0;
       for(int i=0;i<length;i++){
           if(isdigit(password[i])){
               count++;
           }
       }
       if(count>0){
           return true;
       }
       return false;
   }

   bool checkPunctuation(){
       int count=0;
       for(int i=0;i<length;i++){
           if(ispunct(password[i])){
               count++;
           }
       }
       if(count>0){
           return true;
       }
       return false;
   }


   bool checkIfValidPassword(){
       if(!lengthCheck()){
           cout<<"Error!!! Password length should be between 6 to 20"<<endl;
           return false;
       }

       if(!checkUpperCase()){
           cout<<"Error!!! Password should contains atleast one upper case character"<<endl;
           return false;
       }

       if(!checkLowerCase()){
           cout<<"Error!!! Password should contains atleast one lower case character"<<endl;
           return false;
       }
       if(!checkDigit()){
           cout<<"Error!!! Password should contains atleast one digit character"<<endl;
           return false;
       }
       if(!checkPunctuation()){
           cout<<"Error!!! Password should contains atleast one punctuation character"<<endl;
           return false;
       }
       return true;
   }

};
int main() {
   while(true){
       char passIn[1000];
       cout<<"Enter password : ";
       cin>>passIn;
       Password myPassword(passIn);
       if(myPassword.checkIfValidPassword()){
           cout<<"Password is valid"<<endl;
           break;
       }
   }
   return 0;
}

output:

Console X terminated> CPP Workspace.exe [C/C++ Application] C:1Users Mohammad Shahrukh\Desktoplc langua Enter password: hello

Add a comment
Know the answer?
Add Answer to:
How do I format this into C++? This the prompt: Design a class named Password that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in C++, Design a program that asks the user to enter a password, and then analyze...

    in C++, Design a program that asks the user to enter a password, and then analyze the password, and then analyze the password for the following weaknesses: 1. Fewer than 8 characters 2. Does not contain at least one uppercase letter and one lowercase latter 3. Does not contain at least one numeric digit 4. Does not contain at least one special character( a character that is not a letter or numeric digit) 5. Is a sequence of consecutive uppercase...

  • FOR JAVA: Summary: Write a program to assess password stringency. The solution should be named Password.java....

    FOR JAVA: Summary: Write a program to assess password stringency. The solution should be named Password.java. Include these steps: Write an application that provides the criteria for a strong password, and accepts a user's password from an input dialog box. If the entered password is less than six characters, more than 10 characters, or does not contain at least one uppercase letter and one digit, prompt the user to enter again. When the user's entry meets all the password requirements,...

  • Write a program that asks the user to enter a password, and then checks it for...

    Write a program that asks the user to enter a password, and then checks it for a few different requirements before approving it as secure and repeating the final password to the user. The program must re-prompt the user until they provide a password that satisfies all of the conditions. It must also tell the user each of the conditions they failed, and how to fix it. If there is more than one thing wrong (e.g., no lowercase, and longer...

  • I just started working on some code for a password creator. I was doing some testing and I keep g...

    I just started working on some code for a password creator. I was doing some testing and I keep getting this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6 at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47) at java.base/java.lang.String.charAt(String.java:693) at PasswordCreater.check(PasswordCreater.java:56) at Password.main(Password.java:23 In my code I am having the user create a password that is at least 6 characters long, but no more than 16. Also, include at least one lowercase letter, one uppercase letter, one number digit and one special...

  • In C Write a function that asks for the user's first, middle, and last names. The...

    In C Write a function that asks for the user's first, middle, and last names. The names will be entered by the user on a single line and will be stored in three different C-strings. The program should then store, in a fourth array, the name arranged in the following manner: the last name followed by a comma and a space, followed by the first name and a space, followed by the middle name. For example, if the user entered...

  • C++ Language Overview: Design an APP for a basketball team with a unique name. There should be at least 10 players. The member variables required include team member’s names, jersey number, position p...

    C++ Language Overview: Design an APP for a basketball team with a unique name. There should be at least 10 players. The member variables required include team member’s names, jersey number, position played, and at least 3 stats. Positions are small forward, power forward, center, shooting guard and point guard. 1. A.) Design a class with member variables and appropriate member functions Data should be stored in a read/write file consisting of team member’s names, jersey number, position played, and...

  • You're to write a C++ program that analyzes the contents of an external file called data.txt....

    You're to write a C++ program that analyzes the contents of an external file called data.txt. (You can create this file yourself using nano, that produces a simple ASCII text file.) The program you write will open data.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation and whitespace characters contained in the file so that the caller can report the results to stdout. Additionally, the function will return the total number of characters read to...

  • Using Python INST-FS-IAD-PROD.INS LAB1 Lab: Create User Account 2. get-password() #promt the user and create password, check the password fits the requirement USE the EXACT file names! Create a user...

    Using Python INST-FS-IAD-PROD.INS LAB1 Lab: Create User Account 2. get-password() #promt the user and create password, check the password fits the requirement USE the EXACT file names! Create a user login system. Your code should do the following: 3, create-user_name() #use this function to create the user name 1.Create your user database called "UD.txt", this should be in CSV format 4, write-file() #user this function to save the user name and password into "UD.txt" Deliverables: Sample: the data you saved...

  • 10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string...

    10. replaceSubstring Function Write a function named replaceSubstring. The function should accept three C-string or string object arguments. Let's call them string1, string2, and string3. It should search string for all occurrences of string2. When it finds an occurrence of Programming Challenges string2, it should replace it with string. For example, suppose the three arguments have the following values: stringt: "the dog jumped over the fence" string 2 "the" string3: "that" With these three arguments, the function would return a...

  • Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class...

    Construct a C++ class named Rectangle that has floating-point data members named length and width.  The class should have a zero-argument constructor that initializes each data member to 0. It should have member functions named calcPerimeter() and calcArea() that calculate the perimeter and area of a rectangle respectively, a member function setLength() and setWidth() to set the length and width, member functions getLength() and getWidth() to return the length and width, and a member function showData() that displays the rectangle’s length,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT