Question

In: Computer Science

Write a C++ program that checks if the password is correct. The password is a 4-digit...

Write a C++ program that checks if the password is correct. The password is a 4-digit number combination. The program repeats to ask the password until the password is correct or you enter -1 to exit.

Input: The password is set to ‘1123’. A user input the password to proceed, or -1 to exit.

Sample Output: The program should display the following output. (The red text is a user input.)

Test case 1: when you enter the correct password.

Enter the password (or -1 to exit): 1234

Password is incorrect.

Enter the password (or -1 to exit): 1123

Password is correct. Test case 2: when you exit the program.

Enter the password (or -1 to exit): -1

You exit the program.

Solutions

Expert Solution

/* C++ program that checks if the password is correct.

The program repeats to ask the password until the password is correct or the user enter -1 to exit. */

#include <iostream>

using namespace std;

int main()

{

       int password = 1123; // correct password

       int user_password;

       // input of user password

       cout<<"Enter the password (or -1 to exit): ";

       cin>>user_password;

       // loop continues till user exits

       while(user_password != -1)

       {

             // check if password is correct, print success message and then exit from loop

             if(password == user_password)

             {

                    cout<<"Password is correct"<<endl;

                    break;

             }else // print failure message

                    cout<<"Password is incorrect."<<endl;

             // input of password

             cout<<"Enter the password (or -1 to exit): ";

             cin>>user_password;

       }

       // check if user exited the program

       if(user_password == -1)

             cout<<"You exit the program."<<endl;

               return 0;

}

//end of program

Output:


Related Solutions

In PYTHON: Write a program that asks for a password from a user and checks if...
In PYTHON: Write a program that asks for a password from a user and checks if it satisfies the conditions: Contains at least one lowercase letter (a-z). Contains at least one uppercase letter (A-Z). Contains at least one digit (0-9). Minimum length 6 characters. Contains no spaces. If the password is valid, then the program prints “password accepted”. Otherwise, it keeps asking for more passwords from the user until a valid password is obtained. Hint: You might like to check...
Task Intro: Password JAVA. Write a method that checks a password. The rules for the password...
Task Intro: Password JAVA. Write a method that checks a password. The rules for the password are: - The password must be at least 10 characters. - The password can only be numbers and letters. - Password must have at least 3 numbers. Write a test class that tests the checkPassword method. Hint: You can (really should) use method from built-in String class: public boolean matches(String regex) to check that the current string matches a regular expression. For example, if...
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...
Task Intro: Password JAVA and JUnit5(UNIT TESTING) Write a method that checks a password. The rules...
Task Intro: Password JAVA and JUnit5(UNIT TESTING) Write a method that checks a password. The rules for the password are: - The password must be at least 10 characters. - The password can only be numbers and letters. - Password must have at least 3 numbers. Write a test class(Junit5/Unit testing) that tests the checkPassword method. Hint: You can (really should) use method from built-in String class: public boolean matches(String regex) to check that the current string matches a regular...
Task Intro: Password JAVA and JUnit5(UNIT TESTING) Write a method that checks a password. The rules...
Task Intro: Password JAVA and JUnit5(UNIT TESTING) Write a method that checks a password. The rules for the password are: - The password must be at least 10 characters. - The password can only be numbers and letters. - Password must have at least 3 numbers. Write a test class(Junit5/Unit testing) that tests the checkPassword method. Hint: You can (really should) use method from built-in String class: public boolean matches(String regex) to check that the current string matches a regular...
Write a pro-active password checker that checks to make sure that a user entered password meets...
Write a pro-active password checker that checks to make sure that a user entered password meets certain requirements. You must implement a simple program that prompts the user for two String values, a password and the same password again for confirmation. For the purposes of this lab, a legal password must have all of the following properties: ▪ Length of at least 8 characters ▪ Starts with a lower case letter ▪ Ends with a numerical digit ▪ Has one...
Write a c program that will return 1 if the argument passed is a digit and...
Write a c program that will return 1 if the argument passed is a digit and zero otherwise. Use the following function int isNumber(char *c) to do that. Do not use && or || logical operator. Write a full c program that has the header required and will accept the user entry.
How would I write a program for C++ that checks if an entered string is an...
How would I write a program for C++ that checks if an entered string is an accepted polynomial and if it is, outputs its big-Oh notation? Accepted polynomials can have 3 terms minimum and no decimals in the exponents.
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
4. Construct a flowchart and the C ++ program that, receiving a four-digit integer as data,...
4. Construct a flowchart and the C ++ program that, receiving a four-digit integer as data, determines whether all the digits of the number are even. For example, if the number were 5688, it would not meet the condition since the most significant digit (5) is odd; if on the contrary, the number were 6244, it would be true, since all the digits are even. 5. Make the flowchart and the C ++ program that, when receiving N integers as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT