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:

  1. Request username.
  2. Request password.
  3. Check if username and password is exist/correct.
  4. Display a welcome message and grades.
  5. If username or password is not correct, the user gets an error message with a chance to try again.
  6. A user can attempt login thrice.
  7. 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.";
            }
        }
    }
}