You have a text file, employees.dat, that contains the list of employees: E10203
ID: 3544636 • Letter: Y
Question
You have a text file, employees.dat, that contains the list of employees:
E1020323 42 35.00
E8332423 28 31.50
The values are:
First value is the employee number: The letter E followed by a number.
Second value is the employee's age, an integer
Third value is the employee's hour rate, a double
Each line is 19 bytes: 8 characters for the id, 1 space, 2 for the age, 1 space, and 5 for the rate. The line ends in since it is a Windows text file.
In employee.cpp, wrte code for the class Employee:
You will write code for an Employee class that will be used to represent one line of this data.
Appropriate member variables
Two constructors
One should take one parameter for each member variable and assigns that parameter to the appropriate member variable
The other should take no parameters and initialize all member variables to reasonable default values.
Getters and setters for all member variables.
In your program, write code that:
Computes how many records are in the employee.dat file
Dynamically allocates an array of the appropriate length based on your computation
Reads employee.dat and saves the data into the array.
In the second loop, create the file highEarners.dat and write out all the employees that make $50 or more per hour. Write out all three fields in the class and separate values by spaces.
Clean up all resources.
Explanation / Answer
If you have any doubt , feel free to comment
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
class Employee
{
string employeenumber;
int age;
double rate;
public
:Employee()
{
employeenumber="1";
age=0;
rate=0;
}
Employee(string a, int b, double c)
{
employeenumber=a;
age=b;
rate=c;
}
string getEmployeeNumber()
{
return employeenumber;
}
int getAge()
{
return age;
}
double getRate()
{
return rate;
}
void setEmployeeNumber(string a)
{
employeenumber=a;
}
void setAge(int b)
{
age=b;
}
void setRate(double c)
{
rate=c;
}
};
int main()
{
ifstream file;
file.open("employee.dat");
Employee e;
string enumber;
int eage;
double erate;
int count=0;
while(!file.eof())
{
file>>enumber;
file>>eage;
file>>erate;
count++;
}
file.close();
cout<<"Number of employees "<<count<<endl;
Employee EArray[count];
file.open("employee.dat");
int i=0;
while(!file.eof())
{
file>>enumber;
file>>eage;
file>>erate;
EArray[i].setEmployeeNumber(enumber);
EArray[i].setAge(eage);
EArray[i].setRate(eage);
i++;
}
file.close();
ofstream outfile;
outfile.open("highEarners.dat");
for(i=0;i<count;i++)
{
if(EArray[i].getRate()>=50)
{
outfile<<EArray[i].getEmployeeNumber()<<" "<<EArray[i].getAge()<<" "<<EArray[i].getRate()<<endl;
}
}
outfile.close();
cout<<"Successfully created file"<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.