cs201 assignment 3 solution fall 2019 year 2020

cs201 assignment 3 solution fall 2019 year 2020

 

This is Correct idea solution file is complied my self and found accurate. Please do not copy paste. Write comments of code in your words and style.

 

#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;
void addemployee();
void showemployee();

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();
}
}
// function to show employee data
void updateSalary() {
fstream file(“employee.txt”,ios::binary|ios::in|ios::out);
if(!file) {
cout<<“Error in opening file.\n”;
return;
}
int eCode,sHike;
cout<<“Enter employee code\n”;
cin>>eCode;
cout<<“Salary hike? “;
cin>>sHike;
file.seekg(sizeof(anEmployee)*(eCode-1),ios::beg);
file.read((char*)&anEmployee,sizeof(employee));
anEmployee.setsalary(sHike+anEmployee.getsalary());
file.seekp(sizeof(anEmployee)*(eCode-1),ios::beg);
file.write((char*)&anEmployee,sizeof(employee));
cout<<“Salary updated successfully.\n”;
}
main()
{
ifstream file(“employee.txt”);
if(file) {
file.close();
remove(“employee.txt”);
}
int programOption;
while(true){

 



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;

}
}
}

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: