Question
Q = Make a simple Login program that can take Username and Password and log in the system.
- If the username is incorrect, the program should continue till the user name is input correct.
- If the password is entered wrong three times, the account should be blocked.
How are we going to do?
Certainly, there are many ways of writing this program. However, we are going to discuss probably the simplest way of writing Username & Password Login page system.
Step-by-Step of Making this Program
- First of all, We make the variables.
- We use do-while loop and take an input of username.
- if-else statements have been used to check whether the username is right. If it is, the program proceeds. Otherwise, it goes over to ask Username input again.
- Once Username is correct, the program proceeds to take an input of Password.
- if-else statement is used to check whether the password is right or not. If the password is wrong, the program asks the password for three times and blocks the account if not entered correctly. Otherwise, it logs in the program successfully.
Source Code of Username & Password Login System
string Username; string Password; int Count = 0; do { //Taking Username Inputs cout << "Username: " ; cin >> Username; if (Username == "Admin") // Checking the Username { do { //Password Input cout << "Password: "; cin >> Password; if (Password == "PAdmin") // Checking the Password { cout << "You have Successfully Logged In" << endl; } else{ Count++; if (Count < 3) { cout << "Please Enter the right Password\n"; } else if (Count >= 3) { cout << "Your Account has been blocked!"; } cout << endl; } } while (Count < 3 && Password != "PAdmin"); } else{ cout << "Wrong Username! \nPlease Enter the right Username! \n" << endl; } } while (Count < 3 && Username != "Admin"); cout << endl;