CS201 Assignment No 3 Solution Fall 2019-2020
CS201 Assignment No 3 Solution Fall 2019-2020
CS201 Assignment No 3 Solution Fall 2019-2020
#include<iostream>
#include<fstream>
#include<string.h>
class employee {
int employee_id;
string employee_name;
int employee_salary;
public:
employee(){
}
void setid(int id){
employee_id=id;
}
void setname(string name){
employee_name=name;
}
void setsalary(int salary){
employee_salary=salary;
}
int getid(){
return this->employee_id;
}
string getname(){
return this->employee_name;
}
int getsalary(){
return this->employee_salary;
}
};
employee anEmployee;
void addemployee(){
int id;
cout<<"enter employee ID: ";
cin>>id;
anEmployee.setid(id);
cout<<"enter employee Name: ";
string name;
cin>>name;
anEmployee.setname(name);
int salry;
cout<<"enter employee Salary: ";
cin>>salry;
anEmployee.setsalary(salry);
ofstream empfile("employee.txt",ios::binary|ios::app);
if(!empfile) {
cout << "Cannot open file!" << endl;
return ;
}
empfile.write((char*)&anEmployee, sizeof(employee));
//cout<<anEmployee.getid()<<" "<<anEmployee.getname()<<"
"""<<anEmployee.getsalary();
empfile.close();
if(!empfile.good()) {
cout << "Error occurred at writing time!" << endl;
return ;
}
cout << "Employee Recored added Sucessfully!" << endl;
}
void displayRecord() {
// employee anEmployee;
ifstream file("employee.txt",ios::binary);
if(!file) {
cout<<"Error in opening file.\n";
return;
} else {
cout<<"ID"<<" "<<"Name"<<"\t"<<"Salary"<<endl;
cout<<"==================================================
====="<<endl;
while(file.read((char*)&anEmployee,sizeof(employee))) {
cout<<anEmployee.getid()<<"
"<<anEmployee.getname()<<"\t"<<anEmployee.getsalary();
cout<<endl;
}
file.close();
}
}
cout<<"\n\n\n";
cout<<"Please select the option Below,.\n";
cout<<"Please enter 1 To ADD AN EMPLOYEE.\n";
cout<<"Please enter 2 To DISPLAY FILE DATA..\n";
cout<<"Please enter 3 To INCREASE EMPLOYEE SALARY..\n";
cout<<"Please enter 4 To Exit Program..\n";
cout<<"==================================================
================\n";
cout<<"Please enter program option :";
cin>>programOption;
switch(programOption){
case 1 :
cout<<"ADD AN EMPLOYEE DATA.\n";
addemployee();
break;
case 2 :
cout<<"Showing Employee Data.\n";
displayRecord();
break;
case 3 :
cout<<"Update salary .\n";
updateSalary();
break;
case 4 :
cout<<"Shuting Down the Program..... .\n";
exit(1);
break;
}
}
}