Homework programming problem

Not sure why I am getting errors. Any help would be greatly appreciated. Thank you!

/*
13. Array of Payroll Objects
Design a PayRoll class that has data members for an employee’s hourly pay 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 file 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 employee’s
gross pay, so this information can be displayed. Sample data to test this program can be
found in the payroll.dat file.
*/

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

//create class
class Payroll
{
//private class
private:
double hourlyRate;
double hours;
double grossPay;
//public class
public:
Payroll(double hourlyRate, double hours, double grossPay)
{
hourlyRate = 0;
hours = 0;
grossPay = 0;
}
//double will return hours worked
double getHours(double h)
{
hours = h;
return hours;
}
//double will return the hourly rate
double getRate(double r)
{
hourlyRate = r;
return hourlyRate;
}
//void will get the the gross pay
void getPay(double g)
{
grossPay = g;
return grossPay;
}
};

//main function
int main()
{
//declare variables
double pay, hours, total;
const int employee = 7;
Payroll worker[employee];

//call the file
ifstream datafile;
datafile.open("payroll.dat");

//if the file doesn't open end the program right away
if (!datafile)
{
cout << "Unable to open file: payroll.dat." << endl;
return 0;
}

//else statement if file opens smoothly
else
{
//looping it 7 each employee
for (int i = 0; i < employee; i++)
{
//get the pay and hours for the employee then get the total pay
datafile >> pay;
datafile >> hours;
total = pay * hours;

//cout all the information
cout << "Employee #" << i + 1 << ": " << endl;
cout << "Hourly rate: $" << worker[i].getRate(pay) << endl;
cout << "Hours worked: " << worker[i].getHours(hours) << endl;
cout << "Total Made: " << worker[i].getPay(total) << endl;

cout << endl;
cout << endl;
}
}

//close the file
datafile.close();

return 0;
//end program
}
It looks like you're missing a default constructor for the Payroll class.

Also, it looks like the getPay function is expected to return a double value, not be a void function.

1
2
3
4
5
void getPay(double g)
{
grossPay = g;
return grossPay;
}


I'd also check what the code is doing compared to the assignment specifications.

Design a PayRoll class that has data members for an employee’s hourly pay and number of hours worked.

Doesn't seem like Gross Pay should be a data member.

The program should read the number of hours each employee worked and their hourly pay rate from a file and call class functions to store this information in the appropriate objects.

Sounds like there should be set functions that just assign the value from the file into the data members.

It should then call a class function, once for each object, to return the employee’s gross pay, so this information can be displayed.

Sounds like this member function should calculate the gross pay and return it.
Not sure why I am getting errors.

What sort of errors? If there are compiler errors which you don't understand, post the text of the message here for someone to look at.

At a glance, there are a few things in the code which look wrong, for example here:
1
2
3
4
5
6
//void will get the the gross pay
void getPay(double g)
{
    grossPay = g;
    return grossPay;
}

A function declared as void cannot return a value. If it is to return a double, make it a type double rather than void.

Also, in that and other functions, it is puzzling why there is an input parameter g to the function, and why it is actually setting the value of grossPay.

There are similar questions regarding the other get functions.
For example the function
1
2
3
4
5
    double getHours(double h)
    {
        hours = h;
        return hours;
    }
should look more like this:
1
2
3
4
    double getHours() const
    {
        return hours;
    }

It is declared as const to indicate to both the reader and to the compiler that the function will not modify the state of the object, it simply returns a result.

There is more which appears wrong with the code - but this might be a start.
Topic archived. No new replies allowed.