0% found this document useful (0 votes)
79 views7 pages

CS201 Assignment No 3 Solution Fall 2019-2020

This C++ program allows the user to manage employee records by adding, displaying, and updating them. It defines an Employee class with ID, name, and salary attributes. Functions are created to add new employee objects to a binary file, display all records, and update an existing employee's salary. The main function runs a menu loop that calls these functions based on the user's selection until they choose to exit.

Uploaded by

Sohaib Aslam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views7 pages

CS201 Assignment No 3 Solution Fall 2019-2020

This C++ program allows the user to manage employee records by adding, displaying, and updating them. It defines an Employee class with ID, name, and salary attributes. Functions are created to add new employee objects to a binary file, display all records, and update an existing employee's salary. The main function runs a menu loop that calls these functions based on the user's selection until they choose to exit.

Uploaded by

Sohaib Aslam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CS201 Assignment No 3 Solution Fall 2019 – 2020

#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 might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy