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 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...
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
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
Write a program in c++ using only if statements that prompts the user to enter an...
Write a program in c++ using only if statements that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1 …., and Saturday is 6) then displays today. Also, prompt the user to enter the number of days after today for a future day and display the future day of the week. The future day can be computed as follows: (today + number of days after today) % 7 Sample run...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If...
Write a c++ program that ask a user to enter his name and birthday (YYYY/MM/DD). If the age is greater than 21 print "welcome," and if the age is less than 21 print "sorry." Use input validation to make sure the birthdate was entered correctly.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT