//program to create class Employee
//a) Add new employee
//b) Search Employee record from id
//c) Update an existing record
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
class Employee
{
int id;
char name[20],address[20];
float salary;
public:
void addRecord()
{
cout<<endl<<"Enter ID: ";
cin>>id;
cout<<"Enter name: ";
cin>>name;
cout<<"Enter address: ";
cin>>address;
cout<<"Enter salary: ";
cin>>salary;
}
void displayRecord()
{
cout<<endl<<"ID: "<<id;
cout<<endl<<"Name: "<<name;
cout<<endl<<"Address: "<<address;
cout<<endl<<"Salary: "<<salary;
}
int getID()
{
return id;
}
};
int main()
{
Employee e;
char option,ch;
int searchID,isFound,updateID,recordID,location;
fstream file("Menu driven Employee.txt",ios::in | ios::out | ios::binary | ios::app | ios::ate);
do
{
cout<<endl<<"**********Menu**********";
cout<<endl<<"Enter your option: ";
cout<<endl<<" a => Add new Employee";
cout<<endl<<" s => Search Employee record from ID";
cout<<endl<<" u => Update an existing record";
cout<<endl<<" x => Exit"<<endl;
cin>>option;
switch(option)
{
case 'a':
cout<<endl<<"Adding new record......";
e.addRecord();
file.seekp(0,ios::end);
file.write((char*)&e,sizeof(e));
break;
case 's':
isFound=0;
cout<<endl<<"Enter ID to be searched.....";
cin>>searchID;
file.seekg(0,ios::beg);
file.read((char*)&e,sizeof(e));
while(file.eof()==0)
{
if(e.getID()==searchID)
//a) Add new employee
//b) Search Employee record from id
//c) Update an existing record
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
class Employee
{
int id;
char name[20],address[20];
float salary;
public:
void addRecord()
{
cout<<endl<<"Enter ID: ";
cin>>id;
cout<<"Enter name: ";
cin>>name;
cout<<"Enter address: ";
cin>>address;
cout<<"Enter salary: ";
cin>>salary;
}
void displayRecord()
{
cout<<endl<<"ID: "<<id;
cout<<endl<<"Name: "<<name;
cout<<endl<<"Address: "<<address;
cout<<endl<<"Salary: "<<salary;
}
int getID()
{
return id;
}
};
int main()
{
Employee e;
char option,ch;
int searchID,isFound,updateID,recordID,location;
fstream file("Menu driven Employee.txt",ios::in | ios::out | ios::binary | ios::app | ios::ate);
do
{
cout<<endl<<"**********Menu**********";
cout<<endl<<"Enter your option: ";
cout<<endl<<" a => Add new Employee";
cout<<endl<<" s => Search Employee record from ID";
cout<<endl<<" u => Update an existing record";
cout<<endl<<" x => Exit"<<endl;
cin>>option;
switch(option)
{
case 'a':
cout<<endl<<"Adding new record......";
e.addRecord();
file.seekp(0,ios::end);
file.write((char*)&e,sizeof(e));
break;
case 's':
isFound=0;
cout<<endl<<"Enter ID to be searched.....";
cin>>searchID;
file.seekg(0,ios::beg);
file.read((char*)&e,sizeof(e));
while(file.eof()==0)
{
if(e.getID()==searchID)
{
cout<<endl<<"The searched record is:"<<endl;
e.displayRecord();
isFound=1;
break;
}
file.read((char*)&e,sizeof(e));
}
file.clear();
if(isFound==0)
cout<<endl<<"*** Data not Found ***"<<endl;
file.close();
break;
case 'u':
recordID=0;
cout<<endl<<"Record is being updated.....";
cout<<endl<<"Enter employee ID to be updated: ";
cin>>updateID;
isFound=0;
file.seekg(0,ios::beg);
file.read((char*)&e,sizeof(e));
while(file.eof()==0)
{
recordID++;
if(e.getID()==updateID)
{
cout<<endl<<"The old record is:";
e.displayRecord();
isFound=1;
break;
}
file.read((char*)&e,sizeof(e));
}
if(isFound==0)
{
cout<<endl<<"Data Not Found";
break;
}
file.clear();
location=(recordID-1)*sizeof(e);
file.seekp(location,ios::beg);
cout<<endl<<"Enter new record to be updated";
e.addRecord();
file.write((char*)&e,sizeof(e));
file.close();
break;
case 'x':
exit(0);
break;
default:
cout<<endl<<"Invalid Option";
cout<<endl<<"The searched record is:"<<endl;
e.displayRecord();
isFound=1;
break;
}
file.read((char*)&e,sizeof(e));
}
file.clear();
if(isFound==0)
cout<<endl<<"*** Data not Found ***"<<endl;
file.close();
break;
case 'u':
recordID=0;
cout<<endl<<"Record is being updated.....";
cout<<endl<<"Enter employee ID to be updated: ";
cin>>updateID;
isFound=0;
file.seekg(0,ios::beg);
file.read((char*)&e,sizeof(e));
while(file.eof()==0)
{
recordID++;
if(e.getID()==updateID)
{
cout<<endl<<"The old record is:";
e.displayRecord();
isFound=1;
break;
}
file.read((char*)&e,sizeof(e));
}
if(isFound==0)
{
cout<<endl<<"Data Not Found";
break;
}
file.clear();
location=(recordID-1)*sizeof(e);
file.seekp(location,ios::beg);
cout<<endl<<"Enter new record to be updated";
e.addRecord();
file.write((char*)&e,sizeof(e));
file.close();
break;
case 'x':
exit(0);
break;
default:
cout<<endl<<"Invalid Option";
No comments:
Post a Comment