C++ Insertion, Deletion, Search Etc .. in A Company Records
C++ Insertion, Deletion, Search Etc .. in A Company Records
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
class emp
{
int code;
char name[20];
char department[20];
int salary;
public:
void getdata()
{cout<<"\n Enter Employee Code";
cin>>code;
cout<<"\nEnter Employee Name";
gets(name);
cout<<"\nEnter Department";
gets(department);
cout<<"\nEnter salary";
cin>>salary;
}
void displaydata()
{cout<<" \n \t\t Employee Data \n";
cout<<" \nEmployee Code: "<<code;
cout<<"\nEmployee Name: "<<name;
cout<<"\nEmployee Department: "<<department;
cout<<"\nEmployee Salary: "<<salary;}
int foundcode()
{return code;}
};
void main()
{
clrscr();
emp A;
char ch;
int choice,n,flag;
getch();
clrscr();
cout<<"\nMenu";
cout<<"\n1. Create a new database";
cout<<"\n2. Add an employee";
cout<<"\n3. Search an employee";
cout<<"\n4. Delete an employee";
cout<<"\n5. Display employee details";
cout<<"\n6. Modify an employee's details";
cout<<"\n7. Exit";
cout<<"\nEnter your choice.. (1-7)";
cin>>choice;
clrscr();
switch(choice)
{
case 1:
ofstream fout("pre.dat", ios::binary);
cout<<"\nEnter the number of employees";
cin>>n;
for(int i =0 ; i < n ; ++i)
{A.getdata();
fout.write((char*)&A,sizeof(A));}
cout<<"\nThe New Database has been created";
fout.close();
break;
case 2:
ofstream file;
file.open("pre.dat",ios::app);
char c;
do
{A.getdata();
file.write((char*)&A,sizeof(A));
cout<<"\nThe record has been added";
cout<<"\nDo you want to add more records?(( Enter 'y' or 'n' ";
cin>>c;
}while(c=='y'||c=='Y');
file.close();
break;
case 3:
cout<<"\nEnter Employee Code to be searched";
int ccode;
cin>>ccode;
ifstream fil;
fil.open("pre.dat");
fil.read((char*)&A,sizeof(A));
while(fil)
{if(A.foundcode()==ccode)
A.displaydata();
fil.read((char*)&A,sizeof(A));}
fil.close();
break;
case 4:
cout<<"\nEnter employee's code which is to be deleted";
int dcode;
cin>>dcode;
fstream fin;
fstream nfile;
fin.open("pre.dat",ios::in);
nfile.open("new.dat",ios::out);
fin.read((char*)&A,sizeof(A));
flag=0;
while(fin)
{if (A.foundcode()!=dcode)
nfile.write((char*)&A,sizeof(A));
else
flag = 1;
fin.read((char*)&A,sizeof(A));
}if(flag==1)
{remove("pre.dat");
rename("new.dat","pre.dat") ;
cout<<"\nRecord is deleted";
}
else
cout<<"\nRecord could not be found";
break;
case 5:
ifstream fie;
fie.open("pre.dat");
fie.read((char*)&A,sizeof(A));
while(fie)
{A.displaydata();
fie.read((char*)&A,sizeof(A));
}
fie.close();
break;
case 6:
int empno;
long int x;
cout<<"\nEnter the employee code you want to modify";;
cin>>empno;fstream fon("pre.dat",ios::in|ios::out);
while(fon)
{
x=fon.tellg();
fon.read((char*)&A,sizeof(A));
if(A.foundcode()==empno)
{A.getdata();
fon.seekg(x);
fon.write((char*)&A,sizeof(A));
cout<<"\nEmployee Datails have been modified";}
}
break;
case 7:
exit(0);
default:
cout<<"\nYou entered the wrong option.....";
}
cout<<"\nDo you wish to continue";
cin>>ch;
}