Tutorial: Creating a Basic Employee Login and Registration System in C++

Soubhagyajit Borah
Soubhagyajit BorahNovember 24, 2023


In this tutorial, we'll create a simple console-based employee login system using C++. The program allows employees to sign up, log in, display employee details, and exit the system.


Purpose:


The purpose of this tutorial is to guide you through the creation of a simple console-based employee login system using C++. This system allows employees to sign up, log in, display employee details, and exit the program. It serves as a foundation for understanding basic concepts in C++ programming, including class definition, user input, conditional statements, loops, and program flow control.


Key Features:


  1. Employee Class: We define an Employee class to encapsulate the properties and behaviors related to employee data. Private members include an ID and an array to store passwords, while public members include arrays for the first letter of the name and functions for signing up, logging in, displaying employee details, and making choices.
  2. Signup Function: The signup function allows employees to register by providing their name's first letter and a password. Each employee is assigned a unique ID, and successful registration prompts the user to make further choices.
  3. Choice Function: The choice function presents a menu to the user, offering options to display all employees, add a new employee, or exit the program. Depending on the user's choice, it calls the appropriate function or exits the program.
  4. Login Function: The login function simulates the login process, prompting the user to enter an ID, the first letter of their name, and a password. It checks if the entered credentials match any existing employee's data. If successful, it informs the user, and if unsuccessful, it prompts the user to retry.
  5. Display Function: The display function iterates through existing employee data and prints their details. After displaying the details, it calls the choice function to allow the user to perform additional actions.
  6. Main Function: In the main function, we create an instance of the Employee class. Using an infinite loop, we repeatedly prompt the user to log in and perform actions. The loop continues until the user decides to exit the program.


Step 1: Define the Employee Class

#include <iostream>
using namespace std;

class Employee
{
private:
  int id = 1, pswd[100];

public:
  char nameL[100];
  void signup(void);
  void login(void);
  void display(void);
  void choice(void);
};
  • We define a Employee class that encapsulates the properties and behaviors related to employee data.
  • Private members: id for employee ID, pswd array to store passwords.
  • Public members: nameL array for storing the first letter of the employee's name, and functions signup, login, display, and choice.



Step 2: Implement the Signup Function

void Employee::signup()
{
  cout << "Enter the first letter of your name: ";
  cin >> nameL[id];
  cout << "Enter the password: ";
  cin >> pswd[id];
  cout << "\nNew record added!" << endl;
  cout << "Your id is " << id << "\nPlease note down the id as it will be required at the time of login" << endl;
  id++;
  login();
}
  • The signup function allows an employee to register by inputting their name's first letter and a password.
  • The employee is assigned an ID, and the user is informed about the successful registration.
  • The function then calls the login function to prompt the user for perform a successful login.


Step 3: Implement the Login Function

void Employee::login()
{
  char l; // to store the first letter
  int p, i; // to store the password and id that was given to the user on signing up
  cout << "\nWelcome back!\n" << endl;

  cout << "Login to your account\n";
  cout << "Enter your id number: ";
  cin >> i;
  cout << "Enter the first letter of your name: ";
  cin >> l;
  cout << "Enter the password: ";
  cin >> p;

  if (i >= 1 && i < id && nameL[i] == l && pswd[i] == p)
  {
    cout << "Login successful!\n";
    choice();
  }
  else
  {
    cout << "Invalid credentials\n";
    login(); // Retry login if credentials are invalid
  }
}
  • The login function simulates the login process:The user is prompted to enter their ID, the first letter of their name, and the password.
  • The function checks if the entered credentials match any existing employee's data.
  • If successful, it informs the user and calls the choice function.
  • If unsuccessful, it informs the user of invalid credentials and prompts them to retry by calling itself.


Step 4: Implement the Choice Function

void Employee::choice()
{
  int o;
  cout << "Select an option below:\n1. Display all employees\n2. New employee\n3. Exit\n";
  cout << "Enter your choice (1, 2, or 3): ";
  cin >> o;
  switch (o)
  {
  case 1:
    display();
    break;
  case 2:
    signup();
    break;
  case 3:
    cout << "Exiting program. Goodbye!\n";
    exit(0);
    break;
  default:
    cout << "Wrong option! Please try again.\n";
    choice();
  }
}
  • The choice function provides a menu for the user to choose from different options:Option 1: Display all employees (calls display function).
  • Option 2: Add a new employee (calls signup function).
  • Option 3: Exit the program.



Step 5: Implement the Display Function

void Employee::display()
{
  for (int i = 1; i < id; i++)
  {
    cout << "\nFor id " << i << " the first letter is " << nameL[i] << " and password is " << pswd[i];
  }
  cout << endl;
  choice();
}
  • The display function iterates through existing employee data and prints their details.
  • It then calls the choice function to allow the user to perform additional actions.



Step 6: Implement the Main Function

int main()
{
  Employee employee;
  while (true)
  {
    employee.login();
  }
  return 0;
}
  • In the main function, we create an instance of the Employee class.
  • We use an infinite loop to repeatedly prompt the user to log in and perform actions.
  • The loop continues until the user decides to exit the program.



Conclusion:

This tutorial provides a basic structure for a console-based employee login system in C++. You can further enhance the system by adding features such as data persistence (e.g., storing data in a file), password encryption, and more advanced user interfaces.

Feel free to modify and update the code.


Downloads :

Login and Registration system in C++ code

Leave a Comment

Comments (0)

No comments

Notice: I am currently updating the UI of the website. If you notice any bug on the website, don't hesitate to tell me 😊.

Also if you experience any issue just reload the site.

Soubhagyajit Borah
Developed by Soubhagyajit Borah ã…¤
© Copyright . All right reserved.