Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream> //pgm on employee using files
- #include<string.h>
- #include<fstream>
- using namespace std;
- class employee
- {
- int no;
- char name[20],des[20],dep[20];
- float sal;
- public:
- void input()
- {
- cout<<"\nEnter employee no., name, designation, department and salary: ";
- cin>>no>>name>>des>>dep>>sal;
- }
- void display()
- {
- cout<<no<<"\t"<<name<<"\t"<<des<<"\t\t"<<dep<<"\t"<<sal<<"\n";
- }
- char* getdep()
- {
- return dep;
- }
- int getno()
- {
- return no;
- }
- char* getdes()
- {
- return des;
- }
- void modsal()
- {
- cout<<"\nEnter salary: ";
- cin>>sal;
- }
- };
- int main()
- {
- int opt, n, i, j, empno, flag=0;
- employee e, t[10], temp;
- char ch, d[20];
- cout<<"\nEnter number of employees: ";
- cin>>n;
- fstream f;
- f.open("Employee.dat",ios::out|ios::binary);
- for(i=0;i<n;i++)
- {
- e.input();
- f.write((char*)&e,sizeof(e));
- }
- f.close();
- do
- {
- cout<<"\n1.Search for employee"
- "\n2.display employees in a specific department"
- "\n3.Sort employees according to designation"
- "\n4.Modify salary of an employee"
- "\nEnter choice: ";
- cin>>opt;
- switch(opt)
- {
- case 1:
- cout<<"\nEnter employee no.: ";
- cin>>empno;
- f.open("Employee.dat",ios::in|ios::binary);
- for(i=0;i<n;i++)
- {
- f.read((char*)&e,sizeof(e));
- if(empno==e.getno())
- {
- flag=1;
- cout<<"\nE.no\tName\tDesignation\tDepartment\tSalary\n";
- e.display();
- break;
- }
- }
- f.close();
- if(flag==0)
- cout<<"\nRecord not found!!";
- break;
- case 2:
- cout<<"\nEnter department: ";
- cin>>d;
- f.open("Employee.dat",ios::in|ios::binary);
- for(i=0;i<n;i++)
- {
- f.read((char*)&e,sizeof(e));
- if(strcmp(d,e.getdep())==0)
- {
- flag=1;
- e.display();
- }
- }
- f.close();
- if(flag==0)
- cout<<"\nRecord not found!!";
- break;
- case 3:
- f.open("Employee.dat",ios::in|ios::binary);
- for(i=0;i<n;i++)
- {
- f.read((char*)&e,sizeof(e));
- t[i]=e;
- }
- for(i=0;i<n;i++)
- for(j=0;j<n-1-i;j++)
- if(strcmp(t[j].getdes(),t[j+1].getdes())>0)
- {
- temp=t[j];
- t[j]=t[j+1];
- t[j+1]=temp;
- }
- cout<<"\nE.no\tName\tDesignation\tDepartment\tSalary\n";
- for(i=0;i<n;i++)
- t[i].display();
- f.close();
- break;
- case 4:
- cout<<"\nEnter employee no.: ";
- cin>>empno;
- f.open("Employee.dat",ios::in|ios::out|ios::binary);
- for(i=0;i<n;i++)
- {
- f.read((char*)&e,sizeof(e));
- if(empno==e.getno())
- {
- e.modsal();
- f.write((char*)&e,sizeof(e));
- cout<<"\nE.no\tName\tDesignation\tDepartment\tSalary\n";
- e.display();
- break;
- }
- }
- f.close();
- break;
- default: cout<<"\nInvalid option!";
- }
- cout<<"\nDo you want to continue?[y/n]: ";
- cin>>ch;
- }
- while(ch=='y');
- }
Add Comment
Please, Sign In to add comment