CodeWithRandom logo
4 min readCodeWithRandom

Creating an Employee Database Management project using C++

In this article, we will learn how to create an Employee Database Management project using C++. We will be creating a C++-based employee database management system

Creating an Employee Database Management project using C++

Introduction

Hello, coders. Welcome to the codewithrandom blog. In this article, we will learn how to create an Employee Database Management project using C++.

We will be creating a C++-based employee database management system which is a menu-driven application that allows us to perform these operations – Add, list, Modify and Delete personnel information in an organization.

Employee Database Management project using C++

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <conio.h>
#include <iomanip>

using namespace std; int main() { FILE *fp, *ft; char another, choice;

struct employee                //declaration of structure: structure may contain different data type
{
    char first_name[50], last_name[50];
    int Age;
    long Salary;
};

struct employee e;                            
char xfirst_name[50], xlast_name[50];
long int recsize;

fp=fopen("users.txt","rb+");

if (fp == NULL)
{
    fp = fopen("users.txt","wb+");

    if (fp==NULL)
    {
        puts("Cannot open file");
        return 0;
    }
}



recsize = sizeof(e);

while(1)
{
    system("cls");

    cout &lt;&lt; "\t\t====== Employee DATABASE MANAGEMENT SYSTEM ======";
    cout &lt;&lt;"\n\n                                          ";
    cout &lt;&lt; "\n\n";
    cout &lt;&lt; "\n \t\t\t 1. Add    Records";
    cout &lt;&lt; "\n \t\t\t 2. List   Records";
    cout &lt;&lt; "\n \t\t\t 3. Modify Records";
    cout &lt;&lt; "\n \t\t\t 4. Delete Records";
    cout &lt;&lt; "\n \t\t\t 5. Exit   Program";
    cout &lt;&lt; "\n\n";
    cout &lt;&lt; "\t\t\t Select Your Choice :=&gt; ";
    fflush(stdin);                                       // temporary specific data store for certain tme
    choice = getche();                                   // function takes single character as an input
    switch(choice)
    {
    case '1' :
        fseek(fp,0,SEEK_END);
        another ='Y';
        while(another == 'Y' || another == 'y')
        {
            system("cls");
            cout &lt;&lt; "Enter the Firt Name : ";
            cin &gt;&gt; e.first_name;
            cout &lt;&lt; "Enter the Last Name : ";
            cin &gt;&gt; e.last_name;
            cout &lt;&lt; "Enter the Employee Age     : ";
            cin &gt;&gt; e.Age;
            cout &lt;&lt; "Enter the Employee Salary   : ";
            cin &gt;&gt; e.Salary;
            fwrite(&amp;e,recsize,1,fp);
            cout &lt;&lt; "\n Add Another Record (Y/N) ";
            fflush(stdin);
            another = getchar();
        }
        break;
    case '2':
        system("cls");
        rewind(fp);                                            //sets the file position indicator to the beginning of the given file stream.
        cout &lt;&lt; "=== View the Records in the Database ===";
        cout &lt;&lt; "\n";
        while (fread(&amp;e,recsize,1,fp) == 1)
        {
            cout &lt;&lt; "\n";
            cout &lt;&lt;"\n" &lt;&lt; e.first_name &lt;&lt; setw(10)  &lt;&lt; e.last_name;
            cout &lt;&lt; "\n";
            cout &lt;&lt;"\n" &lt;&lt;e.Age &lt;&lt;  setw(8)  &lt;&lt; e.Salary;
        }
        cout &lt;&lt; "\n\n";
        system("pause");
        break;

    case '3' :
        system("cls");
        another = 'Y';
        while (another == 'Y'|| another == 'y')
        {
            cout &lt;&lt; "\n Enter the last name of the Employee : ";
            cin &gt;&gt; xlast_name;

            rewind(fp);
            while (fread(&amp;e,recsize,1,fp) == 1)
            {
                if (strcmp(e.last_name,xlast_name) == 0)
                {
                    cout &lt;&lt; "Enter new the Firt Name : ";
                    cin &gt;&gt; e.first_name;
                    cout &lt;&lt; "Enter new the Last Name : ";
                    cin &gt;&gt; e.last_name;
                    cout &lt;&lt; "Enter new Employee Age     : ";
                    cin &gt;&gt; e.Age;
                    cout &lt;&lt; "Enter new Employee Salary   : ";
                    cin &gt;&gt; e.Salary;
                    fseek(fp, - recsize, SEEK_CUR);
                    fwrite(&amp;e,recsize,1,fp);
                    break;
                }
                else
                    cout&lt;&lt;"record not found";
            }
            cout &lt;&lt; "\n Modify Another Record (Y/N) ";
            fflush(stdin);
            another = getchar();
        }
        break;


    case '4':
        system("cls");
        another = 'Y';
        while (another == 'Y'|| another == 'y')
        {
            cout &lt;&lt; "\n Enter the last name of the Employee to delete : ";
            cin &gt;&gt; xlast_name;

            ft = fopen("temp.dat", "wb");

            rewind(fp);
            while (fread (&amp;e, recsize,1,fp) == 1)

                if (strcmp(e.last_name,xlast_name) != 0)
                {
                    fwrite(&amp;e,recsize,1,ft);
                }
            fclose(fp);
            fclose(ft);
            remove("users.txt");
            rename("temp.dat","users.txt");

            fp=fopen("users.txt","rb+");

            cout &lt;&lt; "\n Delete Another Record (Y/N) ";
            fflush(stdin);
            another = getchar();
        }

        break;

    case '5':
        fclose(fp);
        cout &lt;&lt; "\n\n";
        cout &lt;&lt; "\t\t     THANKS FOR USING THIS SOFTWARE";
        cout &lt;&lt; "\n\n";
        exit(0);
    }
}


system("pause");
return 0;

}

Conclusion

We have reached the end of this article and have a lot more projects in C++ coming so stay tuned. We have started with more amazing and fun projects for you all. Learning C++ by creating fun projects makes learning easy and interesting.

make an Advanced Calculator Program in C++ (Source Code)

If you enjoyed the article and learned something new today, let us know in the comments.

Thank you.

Happy Reading! 🙂