Issue displaying Gross Pay as $0.00

Design a Payroll class that has data members for an employee's hourly pay rate and the number of hours worked.

Write a program using 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.
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.txt file.


For some reason when I compile my program, The gross pay displays as $0.00. What am I doing wrong?

Header 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
  #pragma once
#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;
	}

	void setHours(double h)
	{
		hours = h;
	}
    void setPayRate(double p)
	{
		payRate = p;
	}

	double getHours()
	{
		return hours;
	}
	double getPayRate()
	{
		return payRate;
	}
	double getGrossPay()
	{
		return grossPay;
	}

};



Source Code
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
#include "Payroll.h"

const int employees = 7;
double totalPay = 0;

int main()
{

	int emp;

	Payroll totalPay[employees];
	{
		fstream inputFile;

		inputFile.open("Payroll.txt");

		
			for (emp = 0; emp < 7; emp++)
			{
				double hours;
				double payRate;
				double grossPay;

				inputFile >> hours;
				inputFile >> payRate;
				grossPay = hours * payRate;

				cout << endl;
				cout << fixed << setprecision(2);
				cout << "Employee" << setw(2) << (emp + 1) << "- Your Gross Pay is: $" << totalPay[employees].getGrossPay() << endl;
				cout << "_________________________________________________" << endl;
			}

		inputFile.close();
	}

	system("pause");
	return 0;
}



Text File
1
2
3
4
5
6
7
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
closed account (E0p9LyTq)
You are reading and updating local variables in main, lines 20-26, you don't use your created object array at all except to printout the default value(s) stored in it.
Topic archived. No new replies allowed.