Write a console-based employee management system through binary File Handling (In order to observe data security, we use binary file format so that data cannot be read directly from the TXT file) which will perform management actions of Employee by taking input from user by showing following three options:
1. Press 1 to ADD AN EMPLOYEE.
2. Press 2 to DISPLAY FILE DATA.
3. Press 3 to INCREASE EMPLOYEE SALARY.
After dealing with each option, show prompt to user and continue the program until user press other than ‘y’

Respuesta :

Answer:

#include <fstream>

#include <iostream>

#define FILE_N "employee.dat"

using namespace std;

class Employee {

private :  

int  empID;

char  empName[100] ;

int  salary;

public  :

void readEmployeeDetails(){

 cout<<" PLEASE SUBMIT EMPLOYEE DETAILS"<<endl;

 cout<<"INPUT EMPLOYEE ID : " ;

 cin>>empID;

 cin.ignore(1);

 cout<<"ENTER EMPLOYEE NAME: ";

 cin.getline(empName,100);

 cout<<"ENTER EMPLOYEE SALARY : ";

 cin>>salary;

}

void increasesalary(){

 cout<<" Increment Salary"<<endl;

 cout<<"ENTER INCREMENT SALARY : ";

 int sal;

 cin>>sal;

 salary=salary + sal;

}

void displayEmployee(){

 cout<<"EMP ID: "<<empID<<endl

  <<"EMP NAME: "<<empName<<endl

  <<"SALARY: "<<salary<<endl;

}

};

int main(){

   int options;

   

LOOP:cout<<"Enter the Options:";

   cin>>options;

if(options == 1)

{

Employee emp;

emp.readEmployeeDetails();

fstream file;

file.open(FILE_N,ios::out|ios::binary);

if(!file){

 cout<<"There is an Error found while creating file...\n";

 return -1;

}

 

file.write((char*)&emp,sizeof(emp));

file.close();

cout<<"Date has been saved in the file.\n";

goto LOOP;

}

else if(options == 2)

{

Employee emp;    

fstream file;

file.open(FILE_N,ios::in|ios::binary);

if(!file){

 cout<<"There is an error while opening file...\n";

 return -1;

}

 

if(file.read((char*)&emp,sizeof(emp))){

  cout<<endl<<endl;

  cout<<"Data being discovered from the file..\n";

  emp.displayEmployee();

}

else{

 cout<<"There is an error while reading the file...\n";

 return -1;

}

goto LOOP;

}

else

{

  Employee emp;

emp.increasesalary();

fstream file;

file.open(FILE_N,ios::out|ios::binary);

if(!file){

 cout<<"Error found while creating file...\n";

 return -1;

}

 

file.write((char*)&emp,sizeof(emp));

file.close();

cout<<"Date saved inside the file.\n";

goto LOOP;

}

return 0;

}

Explanation:

The above program is in C++. It creates a class, assign it value, and then work on three option.

1. Press 1 to ADD AN EMPLOYEE.

2. Press 2 to DISPLAY FILE DATA.

3. Press 3 to INCREASE EMPLOYEE SALARY.

Rest is as shown in program.