Use you project 1 (or similar one that you wrote) with header and cpp in same fi
ID: 3825725 • Letter: U
Question
Use you project 1 (or similar one that you wrote) with header and cpp in same file to write data to a random access (binary) file. Also include a program that reads the file. Please note that instead of string you need to use array of characters. **down below is project 1 please add in to what it is asking for**
#include <iostream>
#include <ctime>
using namespace std;
class Employee
{
private:
string name;
int hiredYear;
int yearsWorked;
double salary;
public:
Employee(string e_name, int e_hiredYear, double e_sal)
{
name = e_name;
hiredYear = e_hiredYear;
salary = e_sal;
time_t t = time(NULL);
tm* timePtr = localtime(&t);
yearsWorked = (1900 + timePtr->tm_year )- hiredYear;
}
void setName(string e_name)
{
name = e_name;
}
void setSalary(double e_sal)
{
salary = e_sal;
}
void setHiredYear(int years)
{
hiredYear = years;
time_t t = time(NULL);
tm* timePtr = localtime(&t);
yearsWorked = (1900 + timePtr->tm_year) - years;
}
string getName()
{
return name;
}
double getSalary()
{
return salary;
}
int getYearsWorked()
{
return yearsWorked;
}
int getHiredYear()
{
return hiredYear;
}
void print()
{
cout<<" Name="<<name<<" HiredYear="<<hiredYear<<" Salary="<<salary;
cout<<" YearsWorked="<<yearsWorked<<endl;
}
};
int main() {
int choice, recordNum, updateChoice, hiredyear;
string name;
double sal;
Employee employees[5]{{"John Smith", 2003, 4000},
{"Sheela Dixit", 1999, 6000},
{"Leena Barman", 2005, 3700},
{"James Moriairty", 2010, 2000},
{"John Watson", 1989, 8000}};
while(true)
{
cout<<"1. Print a record 2. Print all records 3. Update a record 4. Exit";
cout<<" Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: cout<<" Select a record(1-5): ";
cin>>recordNum;
if(recordNum>0 && recordNum <= 5)
employees[recordNum-1].print();
break;
case 2: for(int i=0; i<5; i++)
{
employees[i].print();
}
break;
case 3: cout<<" Select a record(1-5): ";
cin>>recordNum;
if(recordNum>0 && recordNum <= 5)
{
cout<<" 1. Update Name 2. Update salary 3. Update hired year";
cout<<" Enter your choice: ";
cin>>updateChoice;
if(updateChoice == 1)
{
cout<<"Enter the new name: ";
cin>>name;
employees[recordNum-1].setName(name);
}
else if(updateChoice == 2)
{
cout<<"Enter the new salary: ";
cin>>sal;
employees[recordNum-1].setSalary(sal);
}
else if(updateChoice == 3)
{
cout<<"Enter the new hired year: ";
cin>>hiredyear;
employees[recordNum-1].setHiredYear(hiredyear);
}
}
break;
case 4: exit(0);
}
}
return 0;
}
Explanation / Answer
Written the data into a file called Employees_output.txt and changed the string to character array. Cannot understand what to read ?
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
class Employee
{
private:
char * name;
int hiredYear;
int yearsWorked;
double salary;
public:
Employee(char* e_name, int e_hiredYear, double e_sal)
{
name = e_name;
hiredYear = e_hiredYear;
salary = e_sal;
time_t t = time(NULL);
tm* timePtr = localtime(&t);
yearsWorked = (1900 + timePtr->tm_year )- hiredYear;
}
void setName(char* e_name)
{
name = e_name;
}
void setSalary(double e_sal)
{
salary = e_sal;
}
void setHiredYear(int years)
{
hiredYear = years;
time_t t = time(NULL);
tm* timePtr = localtime(&t);
yearsWorked = (1900 + timePtr->tm_year) - years;
}
char* getName()
{
return name;
}
double getSalary()
{
return salary;
}
int getYearsWorked()
{
return yearsWorked;
}
int getHiredYear()
{
return hiredYear;
}
void print()
{
ofstream myfile;
myfile.open ("Employees_output.txt", ios::app);
myfile << "Name = "<<name<<" HiredYear = "<<hiredYear<<" Salary = "<<salary;
myfile <<" YearsWorked = "<<yearsWorked<<endl;
myfile.close();
cout<<" Name = "<<name<<" HiredYear = "<<hiredYear<<" Salary = "<<salary;
cout<<" YearsWorked = "<<yearsWorked<<endl;
}
};
int main()
{
int choice, recordNum, updateChoice, hiredyear;
char name[30];
double sal;
Employee employees[5] {{"John Smith", 2003, 4000},
{"Sheela Dixit", 1999, 6000},
{"Leena Barman", 2005, 3700},
{"James Moriairty", 2010, 2000},
{"John Watson", 1989, 8000}
};
while(true)
{
cout<<"1. Print a record 2. Print all records 3. Update a record 4. Exit";
cout<<" Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<" Select a record(1-5): ";
cin>>recordNum;
if(recordNum>0 && recordNum <= 5)
employees[recordNum-1].print();
break;
case 2:
for(int i=0; i<5; i++)
{
employees[i].print();
}
break;
case 3:
cout<<" Select a record(1-5): ";
cin>>recordNum;
if(recordNum>0 && recordNum <= 5)
{
cout<<" 1. Update Name 2. Update salary 3. Update hired year";
cout<<" Enter your choice: ";
cin>>updateChoice;
if(updateChoice == 1)
{
cout<<"Enter the new name: ";
cin>>name;
employees[recordNum-1].setName(name);
}
else if(updateChoice == 2)
{
cout<<"Enter the new salary: ";
cin>>sal;
employees[recordNum-1].setSalary(sal);
}
else if(updateChoice == 3)
{
cout<<"Enter the new hired year: ";
cin>>hiredyear;
employees[recordNum-1].setHiredYear(hiredyear);
}
}
break;
case 4:
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.