Array of Payroll Objects

Hello All,

I'm new to C++ and currently have this assignment that I cant figure out. I can get the gross pay to display without doing the class object but the assignment question asks to use a class function. As the code is below it just gives me $0.00 for all 7 employees. Below is the assignment question, can anyone give me some hints on what I'm missing or doing wrong here, Thanks!

*********************************** QUESTION ***********************************
Design a PayRoll class that has data members for an employee’s 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 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

class Payroll
{
	private:
		double hours;
		double payrate;
		double grosspay;

	public:
		Payroll ()
		{
			hours = 0.0 ;
			payrate = 0.0;
			grosspay = 0.0;
	
		}

		Payroll (double h, double p)
		{ 
			hours = h;
			payrate = p;
			grosspay = hours * payrate;
		}

		double setHours (double h)
		{
			hours = h;
		}

		double setPayrate (double p)
		{
			payrate = p;
		}

		double getHours ()
		{ return hours; }
		
		double getPayrate ()
		{ return payrate; }

		double getGrosspay ()
		{ return grosspay; }

};

const int employees = 7;

int main ()
{

	int index;

	Payroll totalPay[employees];
	{
		ifstream datafile;

		datafile.open("payroll.dat");

		if (!datafile)
		cout << "Error opening data file \n";

		else
		{
			for (index = 0; index < 7; index++)
			{
				double hours;
				double payrate;
				double grosspay;

				datafile >> hours;
				datafile >> payrate;
				grosspay = hours * payrate;		

			cout << endl;
			cout << fixed << setprecision (2);
			cout << "Employee"  << setw (2) << (index+1) << "- Your Gross Pay is: $" 		
				 << totalPay[employees].getGrosspay () << endl;			
			cout << "***************************************************" << endl;
			}
		}	
		
		datafile.close();
	}		
	
	return 0;
}
		
You initialize 7 Payroll objects on line 57 but you never do change them no matter what your input file contains.

Besides, you do attempt to print the Payroll number 8 -- which does not even exists -- no less than seven times.
Topic archived. No new replies allowed.