How To Store Data of Employees Using Structures in C++

This is the simple program for CMS (Campus Management System) in C++ to store the data employees. The Program stores all data of employees like name, address, phone number, status and salary. There are many ways to store these records and many data structure are available but we are just in learning stage so we used structure to store the data.

Source Code 

# include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;

//Structure
struct employee
{
 char name[20];
 int id;
 char Address[30];
 string phoneNum;
 char status[10];
 int salary;
 char grade[2];
};

//Struct Instance
employee empInstance[50];


//Globel variables
int i=0;
int size = 1;

// Function Declearation
int employee_menu();
void add_employee();

// Main
int main()
{
 cout<<"\a\n\n\n"
  <<"\t*******************************************************"<<endl
  <<"\t**                                                   **"<<endl
  <<"\t**                   Welcome To                      **"<<endl
  <<"\t**                                                   **"<<endl
  <<"\t**       Employee ARRAY and STRUCTURES Demo          **"<<endl
  <<"\t**                                                   **"<<endl
  <<"\t*******************************************************"<<endl
  <<endl
  <<endl;
 cout<<"\t\t\t";
 employee_menu();
 
 return 0;
}//end main
int employee_menu()
{
  system ("color 0F");
 char employee_menu_choice[5];
 cout<<endl<<endl
  <<endl<<endl<<endl
  <<"\a\t  *******************************************************"<<endl
  <<"\t  **                                                   **"<<endl
  <<"\t  **                   You Are In                      **"<<endl
  <<"\t  **                                                   **"<<endl
  <<"\t  **                  Employee Menu                    **"<<endl
  <<"\t  **                                                   **"<<endl
  <<"\t  *******************************************************"<<endl
  <<endl;
 loop_of_employee_menu:
 
  cout<<"\t\t  What do you want to do for employee  "      <<endl
   <<"\t\t  ==================================="      <<endl
   <<endl
   <<     "\n\n\t 1  =>  Add employee  "
   <<     "\n\n\t 2  =>  employee menu Exit \n"
   <<         "\t===     ------------------ "<<endl
   <<"\t\t\t\tYou Select : ";

   cin >> employee_menu_choice;

   i = atoi (employee_menu_choice);
   if (i>0)
   {
    switch (i)
    {
    case 1:
     {
      add_employee();//Function
      break ;
     }
    case 2:
     {
      return 0;
     }break ;
    default :
     {
      return 0;
     }break ;
    }//end switch
   }//end if
   else
   {
    system("CLS");
     
   }//end else
   return 0;
}
void add_employee()
{
 system ("color F0");
 system("CLS");
  cout<<endl<<endl<<endl
  <<endl<<endl<<endl
  <<"\a\t  *******************************************************"<<endl
  <<"\t  **                                                   **"<<endl
  <<"\t  **                  Adding Employee                  **"<<endl
  <<"\t  **                                                   **"<<endl
  <<"\t  *******************************************************"<<endl;
 
 ofstream customer_file;

 cout<<endl
  <<"\t\tCOLLECTING DATA FOR EMPLOYEE NO : "<<size
  <<endl
  <<    "\n\tEnter name terminating at \".\" : ";

 cin.getline(empInstance[size].name,20,'.');//taking name

 cout<<    "\n\tEnter Address terminating at \".\" : ";
 cin.getline(empInstance[size].Address,30,'.');//taking Address
 
 cout<<    "\n\tEnter Phone Number : ";
 cin>>empInstance[size].phoneNum;
 
 cout<<    "\n\tEnter Status terminating at \".\" : ";
 cin.getline(empInstance[size].status,10,'.');//taking status
 
 
 cout<<    "\n\tEnter Salary : ";
 cin>>empInstance[size].salary;

 cout<<    "\n\tEnter Grade terminating at \".\" : ";
 cin.getline(empInstance[size].grade,2,'.');//taking grade
 
 empInstance[size].id = size;//settind customer id

 cout<<endl           
  <<"Customer added with following data"          <<endl
  <<endl
  <<"Name : "<<empInstance[size].name    <<endl
  <<"Adress : "<<empInstance[size].Address    <<endl
  <<"Phone Number : "<<empInstance[size].phoneNum    <<endl
  <<"Grade : "<<empInstance[size].grade    <<endl
  <<"Salary : "<<empInstance[size].salary    <<endl
  <<"Status : "<<empInstance[size].status    <<endl
  <<"ID : "<<empInstance[size].id       <<endl;

 customer_file.open ("customer_data.txt",ios::app);

 if(!customer_file.good())//good() returns 
 {

  customer_file.open ("customer_data.txt",ios::out);//ifnot exist then create

 }

 customer_file <<endl<<empInstance[size].name  ;
 customer_file <<endl<<empInstance[size].Address  ;
 customer_file <<endl<<empInstance[size].phoneNum ;
 customer_file <<endl<<empInstance[size].grade  ;
 customer_file <<endl<<empInstance[size].salary  ;
 customer_file <<endl<<empInstance[size].status  ;
 customer_file <<endl<<empInstance[size].id   ;  

 customer_file.close();

 size++;

}//end of add_employee

Output of the Program

 store the data of an employees using structures



Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment