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...
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 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.
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
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.
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT