13 CPP
13 CPP
OBJECTIVE
A data file EMP.DAT contains objects of class EMPLOYEE with the empId, eName,
eAddress and eSal as data members. Create a menu driven C++ program to do the
followinga)
Add records to the file.
b)
Display all the records.
c)
Accept employee id and display the details.
d)
Modify employee details based on employee id.
e)
Copy the details of employees who are drawing salary more than 10,000 to
another file NEWEMP.DAT. (Also display the new file)
SOURCE CODE
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class employee{
char name[50];
int id;
char address[50];
float salary;
public:
void enter()
{
cout<<"Enter the name of the person: ";
cin.ignore();
cin.getline(name,50);
cout<<"Enter the employee id: ";
cin>>id;
cout<<"Enter the address: ";
cin.ignore();
cin.getline(address,50);
cout<<"Enter the employee salary: ";
cin>>salary;
}
void display()
{
cout<<endl<<name<<"\t"<<id<<"\t"<<address<<"\t"<<salary;
}
friend void searchid();
friend void modifyid();
friend void copy();
};
void add()
{
ofstream f("Employee.dat",ios::binary|ios::app);
employee e;
e.enter();
f.write((char*)&e,sizeof(e));
f.close();
}
void display()
{
ifstream f("Employee.dat");
employee e;
if(!f)
cout<<"File not found";
else
{
while(f.read((char*)&e,sizeof(e)))
{
e.display();
}
}
f.close();
}
void displayemp()
{
ifstream f("Emp.dat");
employee e;
if(!f)
cout<<"File not found";
else
{
while(f.read((char*)&e,sizeof(e)))
{
e.display();
}
}
f.close();
}
void searchid()
{
ifstream f("Employee.dat");
employee e;
int i,flag=0;
if(!f)
cout<<"File not found";
else
{
cout<<"Enter the employee id: ";
cin>>i;
while(f.read((char*)&e,sizeof(e)))
{
if(i==e.id)
{
flag=1;
e.display();
}
}
if(flag==0)
cout<<"Employee record not found";
}
f.close();
}
void modifyid()
{
ifstream f1("Employee.dat");
ofstream f2("Temp.dat",ios::binary);
employee e;
int i,flag=0;
if(!f1)
cout<<"File not found";
else
{
cout<<"Enter the employee id: ";
cin>>i;
while(f1.read((char*)&e,sizeof(e)))
{
if(i==e.id)
{
cout<<"Enter the new details: ";
e.enter();
f2.write((char*)&e,sizeof(e));
flag=1;
}
else
{
f2.write((char*)&e,sizeof(e));
}
}
if(flag==0)
cout<<"Employee record not found";
}
f1.close();
f2.close();
remove("Employee.dat");
rename("Temp.dat","Employee.dat");
}
void copy()
{
ifstream f1("Employee.dat");
ofstream f2("Emp.dat",ios::binary);
employee e;
if(!f1)
cout<<"File not found";
else
{
while(f1.read((char*)&e,sizeof(e)))
{
if(e.salary>10000)
{
f2.write((char*)&e,sizeof(e));
}
}
}
f1.close();
f2.close();
}
void menu()
{
int n;
cout<<"1. Add a new record\n";
cout<<"2. Search for a record based on the employee ID\n";
cout<<"3. Modify a record based on employee ID\n";
cout<<"4. Copy records to a new file(salary greater than
10,000)\n";
cout<<"5. Display all the records\n";
cout<<"6. Display \"Emp.dat\"\n";
cout<<"7. Exit\n";
cout<<"Enter your option: ";
cin>>n;
switch(n)
{
case 1: add();
break;
case 2: searchid();
break;
case 3:modifyid();
break;
case 4:copy();
break;
case 5:display();
break;
case 6:displayemp();
break;
case 7:exit(0);
default:cout<<"Wrong Option";
}
}
int main()
{
start:
menu();
goto start;
getch();
return 0;
OUTPUT
1.
2.
3.
4.
5.
6.
7.
Enter
Enter
Enter
Enter
Enter
your option: 1
the name of the person: James
the employee id: 547
the address: Abu Shagara
the employee salary: 9500
Enter
Enter
Enter
Enter
Enter
your option: 1
the name of the person: Mary
the employee id: 628
the address: Al Nahda
the employee salary: 11600
Enter
Enter
Enter
Enter
Enter
your option: 1
the name of the person: Robert
the employee id: 446
the address: Al Qasimia
the employee salary: 10300
628
Al Nahda
11600
Enter
Enter
Enter
Enter
Enter
Enter
Enter
your option: 3
the employee id: 547
the new details:
the name of the person: James
the employee id: 547
the address: Buhaira
the employee salary: 9500
628
446
Al Nahda
Al Qasimia
11600
10300
547
628
446
Buhaira
Al Nahda
Al Qasimia
9500
11600
10300