Question

In: Computer Science

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 following functions, please add more if needed. Your main function shouldn’t be more than half a page. Notice that most of the functions does not have “size” as an input parameter (why?).

You should have the following variables in main:

int size = 80;

       char * ptrPassword;

Function header

Purpose

char * getInput(const int SIZE)

The function takes user input and save it into the “password” input parameter

bool checkLength(char * password)

The password must be at least six characters long. And no more than 12 characters long

bool checkCase(char * password)

The password must contain at least one uppercase and at least one lower case letter.

bool checkDigit(char * password)

The password must contain at least one digit.

bool checkSymbol(char * password)

The password must contain at least one symbol that is not “alphanumeric”.

bool checkWhiteSpace(char * password)

The password may NOT contain any whitespace characters.

The program should ask for a password and then verify that it meets the stated criteria. If it doesn’t, the program should display a message telling the user why it fails, and then ask for re-entry until it is correct.

Your program should echo the correct password (shown in the sample output)

Sample Output:

Please enter a password:

123

!!!Error!!! The password is too short.

!!!Error!!! The password should contain at least one upper case

!!!Error!!! The password should contain at least one lower case

!!!Error!!! The password should contain at least one symbol

password

!!!Error!!! The password should contain at least one upper case

!!!Error!!! The password should contain at least one digit

!!!Error!!! The password should contain at least one symbol

Please enter a password:

Password

!!!Error!!! The password should contain at least one digit

!!!Error!!! The password should contain at least one symbol

Please enter a password:

Password1

!!!Error!!! The password should contain at least one symbol

Please enter a password:

Password1 #

!!!Error!!! The password cannot contain a space

Please enter a password:

Password1234$

!!!Correct!!! The password you’ve entered is: Password1234$

Solutions

Expert Solution

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;


char * getInput(const int SIZE);
bool checkLength(char * password);
bool checkCase(char * password);
bool checkDigit(char * password);
bool checkSymbol(char * password);
bool checkWhiteSpace(char * password);

int main()
{
    int size = 80;
    char *ptrPassword;
  
    ptrPassword = getInput(size);
  
    bool isCorrect = true;
    if(!checkLength(ptrPassword))
        isCorrect = false;
    if(!checkCase(ptrPassword))
        isCorrect = false;
    if(!checkDigit(ptrPassword))
        isCorrect = false;
    if(!checkSymbol(ptrPassword))
        isCorrect = false;
    if(!checkWhiteSpace(ptrPassword))
        isCorrect = false;
    if(isCorrect){
        cout <<"!!!Correct!!! The password you’ve entered is: ";
        int size = strlen(ptrPassword);
        int i=0;
        for(i=0;i<size;i++)
            cout <<ptrPassword[i];
    }
    return 0;
}

char * getInput(const int SIZE){
    char *password;
    password = new char[SIZE];
    cout << "Please enter a password:" << endl;
    cin >> password;
    return password;
}

bool checkLength(char * password){
    int size =strlen(password);
    if(size<7){
        cout <<"!!!Error!!! The password is too short."<<endl;
        return false;
    }
    else if(size>13){
        cout <<"!!!Error!!! The password is too long."<<endl;
        return false;
    }
    else
        return true;
}

bool checkCase(char * password)
{
   int count = 0; //loop counter
    int size = strlen(password);
    int lcount = 0;
    int ucount = 0;
   while(count<size){
        if(islower(password[count]))
            lcount++;
        if(isupper(password[count]))
            ucount++;
       count++;
   }
   if(lcount==0){
        cout <<"!!!Error!!! The password should contain at least one lower case"<<endl;
   }
   if(ucount==0){
        cout <<"!!!Error!!! The password should contain at least one upper case"<<endl;
   }
   if(lcount==0||ucount==0)
       return false;
   return true;
}

bool checkDigit(char * password){
   int count = 0; //loop counter
    int size = strlen(password);
   while(count<size){
        if(isdigit(password[count]))
            return true;
       count++;
   }
   cout <<"!!!Error!!! The password should contain at least one digit"<<endl;
   return false;
}

bool checkSymbol(char * password){
   int count = 0; //loop counter
    int size = strlen(password);
   while(count<size){
        if(ispunct(password[count]))
            return true;
       count++;
   }
   cout <<"!!!Error!!! The password should contain at least one symbol"<<endl;
   return false;
}

bool checkWhiteSpace(char * password){
   int count = 0; //loop counter
    int size = strlen(password);
   while(count<size){
        if(isspace(password[count])){
            cout <<"!!!Error!!! The password cannot contain a space"<<endl;
            return false;
        }
       count++;
   }
   return true;
}


Related Solutions

Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Write a C++ program to ask the user to enter the shape type: square, sphere or...
Write a C++ program to ask the user to enter the shape type: square, sphere or circle and the appropriate dimensions of the shape.. The program should output the following information about the shape: a.for a square. it output the area and perimeter. b. for a sphere, it outputs the area. c. fir a circle, it outputs the volume. if the user input anything else, your program should output: "the program does not recognize this shape". Use const whenever it...
In.java Write a program that repeatedly asks the user to enter their password until they enter...
In.java Write a program that repeatedly asks the user to enter their password until they enter the correct one. However, after 5 failed attempts, the program "locks out" the user. We will show that with an error message. Assume the password is HOC2141 (case sensitive). Note that there is a special case that is not shown below. To identify it, think of all possible scenarios of input for this program. ----------- Sample run 1: Enter your password: Blake Wrong Enter...
Using Java! Write a program that ask prompt the user to enter a dollar amount as...
Using Java! Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort. Sample Run :- Enter 1 number :- 1 Enter 2 number :- 5 Enter 3 number :- 7 Enter 4 number :- 45 Enter 5 number :- 90 Enter 6 number :- 6 Enter 7 number :- 55...
C++ while loop Exercise Write a program that continues to ask the user to enter any...
C++ while loop Exercise Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total. #include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start...
write a c++ program . Ask the user to enter a number less than 100. Test...
write a c++ program . Ask the user to enter a number less than 100. Test the input to make sure it is correct, and use a while loop to continuously ask the user for correct input value if they don't follow the input rule. After receiving the correct input, test the number to see if it is even or odd. If it is odd, use a while loop to do the following: Output to the monitor all odd numbers...
Using Python write a program that does the following in order: 1. Ask user to enter...
Using Python write a program that does the following in order: 1. Ask user to enter a name 2. Ask the user to enter five numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 3. Calculate the sum of the numbers “amount1”, “amount2”, “amount3”, “amount4”, “amount5” 4. If the sum is greater than 0, print out the sum 5. If the sum is equal to zero, print out “Your account balance is zero” 6. If the sum is less than 0, print out...
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT