HELP! C++ Programming Assignment. I am looking for some help on the second part
ID: 3797214 • Letter: H
Question
HELP! C++ Programming Assignment. I am looking for some help on the second part of my programming assignment.
I was able to complete assignment 1 and received full credit. My next assignment builds onto the first assignment, but I feel lost, I don't know where to start.
Disclaimer: I posted this question twice by ACCIDENT. So if you saw it twice, please forgive me!!!
Here is EmployeeRecord.h code:
#ifndef EMPLOYEERECORD_H
#define EMPLOYEERECORD_H
class EmployeeRecord
{
private:
int m_iEmployeeID;
char m_sFirstName[32];
char m_sLastName[32];
int m_iDeptID;
double m_dSalary;
public:
EmployeeRecord(); //Constructor
EmployeeRecord(int ID, char *fName, char *lName, int dept, double sal); //Setting the values.
~EmployeeRecord(); //Destructor
int getID(); // Return the value stored in the member variable
void setID(int ID);
void getName(char *fName, char *lName);
void setName(char *fName, char *lName);
void getDept(int& d);
void setDept(int d);
void getSalary(double *sal);//Define Pointer Function
void setSalary(double sal);//Copy the function argument to m_dSalary.
void printRecord(); //Print to screen
};
#endif
Here is the EmployeeRecord.cpp code:
#include
#include "EmployeeRecord.h"
#include
#include
#include
#include
#include
using namespace std;
EmployeeRecord::EmployeeRecord()
{
//Set Values
m_iEmployeeID = 0;
m_sFirstName[0] = '';
m_sLastName[0] = '';
m_iDeptID = 0;
m_dSalary = 0.0;
}
EmployeeRecord::EmployeeRecord(int ID, char *fName, char *lName, int dept, double sal)
{
//Problem 1
m_iEmployeeID = ID;
strcpy(m_sFirstName, fName);
strcpy(m_sLastName, lName);
m_iDeptID = dept;
m_dSalary = sal;
}
//Destructor
EmployeeRecord::~EmployeeRecord()
{
}
int EmployeeRecord::getID()
{
return m_iEmployeeID;
}
void EmployeeRecord::setID(int ID)
{
m_iEmployeeID = ID;
}
void EmployeeRecord::getName(char *fName, char *lName)
{
//Problem 2
strcpy(fName, m_sFirstName);
strcpy(lName, m_sLastName);
}
void EmployeeRecord::setName(char *fName, char *lName)
{
strcpy(m_sFirstName, fName);
strcpy(m_sLastName, lName);
}
void EmployeeRecord::getDept(int& d)
{
d = m_iDeptID;
}
void EmployeeRecord::setDept(int d)
{
m_iDeptID = d;
}
void EmployeeRecord::getSalary(double *sal)
{
*sal = m_dSalary;
}
void EmployeeRecord::setSalary(double sal)
{
m_dSalary = sal;
}
void EmployeeRecord::printRecord()
{
cout << "ID: " << m_iEmployeeID << endl;
cout << "First Name: " << m_sFirstName << endl;
cout << "Last Name: " << m_sLastName << endl;
cout << "Department: " << m_iDeptID << endl;
cout << "Salary: " << m_dSalary << endl;
}
I made these files but are currently empty:
CustomerList.h
CustomerList.cpp
Store.h
Store.cpp
These are the requirements for programming assignment 2:
Any help would be greatly appreciated
Explanation / Answer
Class Employee
{
private;
int EmpID;
char EmpName[50],Post[50],Department[10];
float Salary;
public;
void ReadData();
int GetID();
void DisplayRecord();
char* GetDepartment();
};
void Employee::ReadData()
{
cout<<endl<<“Employee ID:”;
cin>>EmpID;
cout<<“Employee Name:”;
cin>>EmpName;
cout<<“Employee’s Post:”;
cin>>Post;
cout<<“Employee’s Department:”;
cin>>Department;
cout<<“Salary:”;
cin>>Salary;
}
void Employee::DisplayRecord()
{
cout<<endl<<“_______________________________”;
cout<<endl<<setw(5)<<EmpID<<setw(15)<<EmpName<<setw(15)<<Post<<setw(15)<<Department<<setw(8)<<Salary;
}
int Employee::GetID()
{
return EmpID;
}
char* Employee::GetDepartment()
{
return Department;
}
void main()
{
Employee emp,e;
char option,ch,Dept[50];
int ID,isFound;
clrscr();
fstream file;
file.open(fileName,ios::ate|ios::in|ios::out|ios::binary);
do
{
cout<<********Menu********;
cout<<endl<<“Enter your option”;
cout<<endl<<“1 => Add a new record”;
cout<<endl<<“2 => Search record from employee id”;
cout<<endl<<“3 => List Employee of particular department”;
cout<<endl<<“4 => Display all employee”;
cout<<endl<<“5 => Update record of an employee”;
cout<<endl<<“6 => Delete record of particular employee”;
cout<<endl<<“7 => Exit from the program”<<endl;
cout<<“********************”<<endl;
cin>>option;
switch(option)
{
case ‘1’:
emp.ReadData();
file.seekg(0,ios::beg);
isFound=0;
file.read((char*)&e,sizeof(e));
while(!file.eof())
{
if(e.GetID()==emp.GetID())
{
cout<<“This ID already exist…Try for another ID”;
isFound=1;
break;
}
file.read((char*)&e,sizeof(e));
}
if(isFound==1)
break;
file.clear();
file.seekp(0,ios::end);
file.write((char*)&emp, sizeof(emp));
cout<<endl<<“New record has been added successfully…”;
break;
case ‘2’:
isFound=0;
cout<<endl<<“Enter ID of an employee to be searched:”;
cin>>ID;
file.seekg(0,ios::beg);
file.read((char*)&e,sizeof(e));
while(!file.eof())
{
if(e.GetID()==ID)
{
cout<<endl<<“The record found….”<<endl;
cout<<endl<<setw(5)<<“ID”<<setw(15)<<“Name”<<setw(15)<<“Post”<<setw(15)<<“Department”<<setw(8)<<“Salary”;
e.DisplayRecord();
isFound=1;
break;
}
file.read((char*)&e,sizeof(e));
}
file.clear();
if(isFound==0)
cout<<endl<<“Data not found for employee ID#”<<ID;
break;
case ‘3’:
isFound=0;
cout<<“Enter department name to list employee within it:”;
cin>>Dept;
file.seekg(0,ios::beg);
file.read((char*)&e,sizeof(e));
while(!file.eof())
{
if(strcmp(e.GetDepartment(),Dept)==0)
{
cout<<endl<<“The record found for this department”<<endl;
cout<<endl<<setw(5)<<“ID”<<setw(15)<<“Name”<<setw(15)<<“Post”<<setw(15)<<“Department”<<setw(8)<<“Salary”;
e.DisplayRecord();
isFound=1;
break;
}
file.read((char*)&e,sizeof(e));
}
file.clear();
if(isFound==0)
cout<<endl<<“Data not found for department”<<Dept;
break;
case ‘4’:
cout<<endl<<“Record for employee”;
file.clear();
file.seekg(0,ios::beg);
int counter=0;
file.read((char*)&e,sizeof(e));
while(!file.eof())
{
counter++;
if(counter==1)
{
cout<<endl<<setw(5)<<“ID”<<setw(15)<<“Name”<<setw(15)<<“Post”<<setw(15)<<“Department”<<setw(8)<<“Salary”;
}
e.DisplayRecord();
file.read((char*)&e,sizeof(e));
}
cout<<endl<<counter<<“records found……”;
file.clear();
break;
case ‘5’:
int recordNo=0;
cout<<endl<<“File is being modified….”;
cout<<endl<<“Enter employee ID to be updated:”;
cin>>ID;
isFound=0;
file.seekg(0,ios::beg);
file.read((char*)&e,sizeof(e));
while(!file.eof())
{
recordNo++;
if(e.GetID()==ID)
{
cout<<“The old record of employee having ID”<<ID<< “is:”;
e.DisplayRecord();
isFound=1;
break;
}
file.read((char*)&e,sizeof());
}
if(idFound==0)
{
cout<<endl<<“Data not found for employee ID#”<<ID;
break;
}
file.clear();
int location=(recordNo-1)*sizeof(e);
file.seekp(location,ios::beg);
cout<<endl<<“Enter new record for employee having ID”<<ID;
e.ReadData();
file.write((char*)&e, sizeof(e));
break;
case ‘6’:
recordNo=0;
cout<<endl<<“Enter employment ID to be deleted:”;
cin>>ID;
isFound=0;
file.seekg(0,ios::beg);
file.read((char*)&e,sizeof(e));
while(!file.eof())
{
recordNo++;
if(e.GetID()==ID)
{
cout<<” The old record of employee having ID “<<ID<< ” is: “;
e.DisplayRecord();
isFound=1;
break;
}
file.read((char*)&e,sizeof(e));
}
char tempFile[]=”temp.txt”;
fstream temp(tempFile,ios::out|ios::binary);
if(isFound==0)
{
cout<<endl<<“Data not found for employee ID#”<<ID;
break;
}
else
{
file.clear();
file.seekg(0,ios::beg);
file.read((char*)&e,sizeof(e));
while(!file.eof())
{
if(eGetID()!=ID)
temp.write((char*)&e,sizeof(e));
file.read((char*)&e,sizeof(e));
}
file.close();
temp.close();
temp.open(tempFile,ios::in|ios::binary);
file.open(fileName,ios::out|ios::binary);
temp.read((char*)&e,sizeof(e));
while(!temp.eof())
{
file.write((char*)&e,sizeof(e));
temp.read((char*)&e,sizeof(e));
}
}
temp.close();
file.close();
remove(tempFile);
file.open(fileName,ios::ate|ios::in|ios::out|ios::binary);
break;
case ‘7’:
exit(0);
break;
default:
cout<<“Invalid Options”;
}
cout<<“ Do you want to continue…..?y/n”;
cin>>ch;
}while(ch!=’n’);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.