Question
C++ I can't seem to get the correct output. My output does not display the first employee's salary at 2 decimal places but it does
C++ I can't seem to get the correct output. My output does not display the first employee's salary at 2 decimal places but it does for other 6 employees. Need it to output 400.00
Program:
13. Array of Payroll Objects
Design a PayRoll class that has data members for an employees hourly pay rate and number of hours worked. Write a program with an array of seven PayRoll objects. The program should read the number of hours each employee worked and their hourly pay rate from a le and call class functions to store this information in the appropriate objects. It should then call a class function, once for each object, to return the employees gross pay, so this information can be displayed. Sample data to test this program can be found in the payroll.dat le.
To create the payroll.dat file, please open notepad, copy/paste the following data and save the file as payroll.dat file. Please make sure to save the file in a place where your program file is located. In otherwords, please do not hard code the path for the payroll.dat .
40.0 10.00 38.5 9.50 16.0 7.50 22.5 9.50 40.0 8.00 38.0 8.00 40.0 9.00
Here is the sample output.
Employee 1: 400.00
Employee 2: 365.75
Employee 3: 120.00
Employee 4: 213.75
Employee 5: 320.00
Employee 6: 304.00
Employee 7: 360.00
Code:
#include
#include
#include
using namespace std;
// define class
class payroll
{
private:
double hoursWorked;
double payRate;
public:
payroll()
{
hoursWorked = 0.00;
payRate = 0.00;
}
void setHour(double x)
{
hoursWorked = x;
}
void setRate(double y)
{
payRate = y;
}
int getHour()
{
return hoursWorked;
}
double getRate()
{
return payRate;
}
double pay()
{
return hoursWorked * payRate;
}
};
int main()
{
// number of workers
payroll workers[7];
// open the file
ifstream infile;
infile.open("payroll.dat");
for (int x = 0; x < 7; x++)
{
double hoursWorked;
double payRate;
// read the file and find values of variables
infile >> hoursWorked;
infile >> payRate;
workers[x].setHour(hoursWorked);
workers[x].setRate(payRate);
}
// close the file
infile.close();
// for loop for all 7 employees
for (int x = 0; x < 7; x++)
{
// display results for each of 7 employee
cout << "Employee " << x + 1 << ": " << workers[x].pay() << endl;
cout << fixed << setprecision(2);
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started