2
Building a simple login system with C++
So I will be making a basic login system with C++. Basically, what the system does is to:
- Request username.
- Request password.
- Check if username and password is exist/correct.
- Display a welcome message and grades.
- If username or password is not correct, the user gets an error message with a chance to try again.
- A user can attempt login thrice.
- After the third invalid trial, the program will output "Access denied! You have exceeded the number of login attempts"
Solution
#include
using namespace std;
int main ()
{
string userId;
string pass;
int secureTry = 0;
int max_attempt = 3;
while(secureTry < max_attempt)
{
cout << "Username: ";
cin >> userId;
cout << "Password: ";
cin >> pass;
if(userId == "guru" && pass == "ads37") {
cout <<"Welcome "<<userId<<".\n\tChemistry: B2\n\tEnglish: A1"<<endl;
break;
}
else if(userId == "prince" && pass == "eng255") {
cout <<"Welcome "<<userId<<".\n\tPhysics: A1\n\tBiology: C5"<<endl;
break;
}
else {
secureTry++;
if(secureTry == max_attempt){
cout <<"Access denied! You have exceeded the number of login attempts"<<endl;
}
else{
cout << "Invalid login! Try again\n.";
}
}
}
}