1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include<conio.h>
#include<iostream>
#include<fstream>
using namespace std;
class Employee
{
private:
char name[20];
char id[20];
int salary;
public:
Employee()
{
strcpy(name,"none");
strcpy(id,"none");
salary=0;
}
void setall(char *n , char *i, int s)
{
strcpy(name,n);
strcpy(id,i);
salary=s;
}
void display()
{
cout<<"name : "<<name<<endl;
cout<<"Id : "<<id<<endl;
cout<<"salary : "<<salary<<endl;
}
char getn()
{
return * name;
}
};
int main()
{
char choice;
char na[20], I[20];
int sal;
Employee e;
fstream file;
file.open("Employee.txt",ios::in|ios::out|ios::binary);
do
{
cout<<"ENter name\n";
cin>>na;
cout<<"Enter id \n";
cin>>I;
cout<<"Enter Salary\n";
cin>>sal;
e.setall(na,I,sal);
file.write(reinterpret_cast<char *>(&e),sizeof(e));
cout<<"Do you want to add another entry y for yes\n";
cin>>choice;
}
while(choice=='y' || choice=='Y');
file.seekg(0);
file.read(reinterpret_cast<char *>(&e),sizeof(e));
while(!file.eof())
{
e.display();
file.read(reinterpret_cast<char *>(&e),sizeof(e));
}
cout<<endl;
cout<<"Enter name to search the employee record\n";
cin>>na;
file.seekg(0);
while(!file.eof())
{
if(strcmp(na,e.getn())==0)
{
e.display();
}
else
cout<<"no record found\n";
}
getch();
}
|