Question

In: Computer Science

Create a C++ program which will prompt the user to enter a password continually until the...

Create a C++ program which will prompt the user to enter a password continually until the password passes the following tests.

Password is 6 chars long

Password has at least 1 number

If the input does not match the tests, it will input a specific error message. "Pass must be 6 chars long."

If the input is allowed, print a message saying "Correct Password Criteria."

Solutions

Expert Solution

C++ Program :

#include <iostream>
using namespace std;

bool validPassword(string s)      // ensures that the password has atleast 1 number in it
{
    int count =0;
    for(int i=0; i<s.length(); i++) // iterate through all characters in string
    {
        if(s[i]>='0' && s[i]<='9') // check if characters are between 0 to 9 (contain digits)
        {
            count++;               // if it contains digits increment count
        }
    }
    if(count>0)              // return true only when there is atleast one number in password
    {
        return true;
    }
    else
    {
        return false;
    }
    
}

int main()
{
    string password;
    while(1)        // infinite loop
    {
        cout << "Please enter a password : ";  // ask for password from user
        cin >> password;
        if((password.length() == 6) && validPassword(password)) // check if password is in correct criteria
        {
            cout << "Correct Password Criteria."<<"\n";    // end the loop if we get correct criteria
            break;
        }
        else                    // print a error message saying specific error
        {
            if(password.length() != 6)        
            {
                cout << "Password must be 6 chars long.\n\n";
            }
            else
            {
                cout << "Password must have atleast 1 number.\n\n";
            }
        }
        
    }
    return 0;
}

Screenshots :


Related Solutions

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...
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.
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
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...
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...
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
5) Create the following in a Java program Create a scanner Prompt the user to enter...
5) Create the following in a Java program Create a scanner Prompt the user to enter the name where the box of mail is shipping from and create the variable and relate to scanner Prompt the user to enter the name of destination where the box of mail will be shipped and create the variable and relate to scanner Prompt the user to enter the weight of the package and create variable to relate to scanner Calculate cost of shipping...
Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the...
Create in C++ Prompt the user to enter a 3-letter abbreviation or a day of the week and display the full name of the day of the week. Use an enumerated data type to solve this problem. Enumerate the days of the week in a data type. Start with Monday and end with Friday. Set all of the characters of the user input to lower case. Set an enumerated value based on the user input. Create a function that displays...
6) Create the following in a Java Program: Create payroll system Prompt the user to enter...
6) Create the following in a Java Program: Create payroll system Prompt the user to enter payroll information Employee name and create variable for scanner Hours worked Hourly pay rate Federal tax rate State tax rate Declare variable doubles for gross pay, federal, state and total deduction Display payroll statement example: Name of employee Hours worked Hourly pay rate Gross pay which is equal hours worked multiply hourly pay rate Federal withholding which is equal of gross pay multiply federal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT