You are on page 1of 7

/*PROGRAM TO CREATE AN EMPLOYEE CLASS AND DISPLAY THE MENU */ #include<fstream.h> #include<conio.h> #include<stdio.h> #include<dos.h> #include<process.

h> char fname[20]; class employee { int empno; char name[20],address[30],pincode[15]; float basic,hra,da,pf; public: void readdata(); int retempno(); void showdata(); }; void employee::readdata() { cout<<"\nEnter employee number: "; cin>>empno; cout<<"\nEnter employee name: "; gets(name); cout<<"\nEnter employee address: "; gets(address); cout<<"\nEnter pincode: "; gets(pincode); cout<<"\nEnter Basic Salary: "; cin>>basic; cout<<"\nEnter HRA: "; cin>>hra; cout<<"\nEnter DA: "; cin>>da; cout<<"\nEnter PF: "; cin>>pf; } int employee::retempno() { return empno; } void employee::showdata() { cout<<"\nEmployee No: "<<empno; cout<<"\nEmployee Name: "<<name; cout<<"\nEmployee Address: "<<address; cout<<"\nEmployee Pin Code: "<<pincode; cout<<"\nEmployee Basic Salary: "<<basic; cout<<"\nEmployee HRA: "<<hra;

cout<<"\nEmployee DA: "<<da; cout<<"\nEmployee PF: "<<pf; } void create() { cout<<"\nEnter file name: "; gets(fname); fstream ofile; ofile.open(fname,ios::out|ios::binary); employee e; char rep; do { e.readdata(); ofile.write((char *)&e,sizeof(employee)); cout<<"\n Want to add more records"; cin>>Rep; }while(rep=='y'); ofile.close(); } void add() { fstream ofile; ofile.open(fname,ios::app|ios::binary); employee e; char ch; do { e.readdata(); ofile.write((char *)&e,sizeof(employee)); cout<<"\nDo you want to add more object(y/n)?: "; cin>>ch; }while(ch=='y'||ch=='Y'); ofile.close(); } void modify() { int eno; cout<<"\nEnter employee number to be modified: "; cin>>eno; employee e1,e2; int countrec=0; fstream file; file.open(fname,ios::in|ios::out|ios::binary); cout<<"\nEnter new details of employee"; e1.readdata(); while(file.read((char*)&e2,sizeof(employee))) {

countrec++; if(eno==e2.retempno()) { cout<<"\nRecord found "; file.seekp((countrec-1)*sizeof(employee),ios::beg); file.write((char*)&e1,sizeof(employee)); cout<<"\nRecord Modified"; break; } } file.close(); } void search() { int eno; cout<<"\nEnter employee number to search: "; cin>>eno; employee e; fstream ifile; ifile.open(fname,ios::in|ios::binary); while(ifile.read((char*)&e,sizeof(employee))) { if(eno==e.retempno()) { e.showdata(); break; } } getch(); } void display() { fstream ifile; ifile.open(fname,ios::in|ios::binary); employee e; while(ifile.read((char *)&e,sizeof(employee))) { e.showdata(); } ifile.close(); cout<<"\Press any key to continue.... "; getch(); } void main() { clrscr(); int ch; char choice;

cout<<"Enter File Name: "; gets(fname); do { clrscr(); cout<<"\n1.Create"; cout<<"\n2.Add"; cout<<"\n3.Modify"; cout<<"\n4.Search"; cout<<"\n5.Display"; cout<<"\n6.Exit"; delay(2000); cout<<"\n\nEnter your choice: "; cin>>ch; switch(ch) { case 1: create(); break; case 2: add(); break; case 3: modify(); break; case 4: search(); break; case 5: display(); break; case 6: exit(0); } cout<<"\nDo you wish to continue(y/n)?: "; cin>>choice; }while(choice=='y'||choice=='Y'); getch(); } /* OUTPUT:Enter File Name: FILEVARUN.CPP 1.Create 2.Add 3.Modify 4.Search 5.Display 6.Exit Enter your choice: 1 Enter file name: FILEVARUN.CPP

Enter employee number: 1 Enter employee name: TARUN Enter employee address: SBI Enter pincode: 90 Enter Basic Salary: 34500 Enter HRA: 3458 Enter HRA: 3458 Enter DA: 45 Enter PF: 345 Do you wish to continue(y/n)?: N2.Add 3.Modify 4.Search 5.Display 6.Exit Enter your choice: 2 Enter employee number: 2 Enter employee name: T Enter employee address: I Enter pincode: 0 Enter Basic Salary: 8 Enter HRA: 78 Enter DA: 8 Enter PF: 9 Do you want to add more object(y/n)?: N Do you wish to continue(y/n)?: Y 1.Create 2.Add 3.Modify 4.Search 5.Display

6.Exit Enter your choice: 3 Enter employee number to be modified: 1 Enter new details of employee Enter employee number: 4 Enter employee name: U Enter employee address: O Enter pincode: 9 Enter Basic Salary: 89 Enter HRA: 8 Enter DA: 6 Enter PF: 7 Record found Record Modified Do you wish to continue(y/n)?: Y 1.Create 2.Add 3.Modify 4.Search 5.Display 6.Exit Enter your choice: 4 Enter employee number to search: 4 Employee No: 4 Employee Name: U Employee Address: O Employee Pin Code: 9 Employee Basic Salary: 89 Employee HRA: 8 Employee DA: 6 Employee PF: 7 Do you wish to continue(y/n)?: Y 1.Create 2.Add 3.Modify

4.Search 5.Display 6.Exit Enter your choice: 5 Employee No: 4 Employee Name: U Employee Address: O Employee Pin Code: 9 Employee Basic Salary: 89 Employee HRA: 8 Employee DA: 6 Employee PF: 7 Employee No: 2 Employee Name: T Employee Address: I Employee Pin Code: 0 Employee Basic Salary: 8 Employee HRA: 78 Employee DA: 8 Employee PF: 9Press any key to continue.... Do you wish to continue(y/n)?: Y 1.Create 2.Add 3.Modify 4.Search 5.Display 6.Exit Enter your choice: 6 */