Payroll program: incorrectly outputs average net pay

I'm a beginner in C++. I'm trying to build this payroll program. It correctly outputs all data (e.g. overtime pay, net pay).

However, I'm trying to get it to output the average of all netpays. The value output for the netpay average is clearly wrong and nonsensical.

I'll be grateful if anyone can point me toward what I'm doing wrong.

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<iostream>
#include<iomanip>
#include<fstream>

int readalldata(long int[], int[], float[], const int);
void findothours(int[], int[], int);
void findotpay(int[], float[], float[], int);
void findreghours(int[], int[], int);
void findregpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[],float, int);
void findsum(float, float[], int);
void findavg(float, float, float, int);
void printalldata(long int[], float[], float[], float[], float[], int);

using std::cout;
using std::cin;
using std::endl;
using std::setw;
using std::left;
using std::setprecision;
using std::fixed;
using std::ifstream;

void main(){
	//DECLARATION OF VARIABLES
	const int MAXSIZE = 100; //maximum of 100 employees
	int n, hworked[MAXSIZE], othours[MAXSIZE], reghours[MAXSIZE];
	long int employeeid[MAXSIZE];
	float hrate[MAXSIZE], regpay[MAXSIZE], otpay[MAXSIZE], grosspay[MAXSIZE], taxrate[MAXSIZE], taxamount[MAXSIZE], netpay[MAXSIZE], sum = 0, average = 0, totalnetpay = 0,x = 0;
	

	//FUNCTION CALLS
	n = readalldata(employeeid, hworked, hrate, MAXSIZE);
	findothours(hworked, othours, n);
	findotpay(othours, hrate, otpay, n);
	findreghours(hworked, reghours, n);
	findregpay(reghours, regpay, hrate, n);
	findgrosspay(regpay, otpay, grosspay, n);
	findtaxrate(grosspay, taxrate, n);
	findtaxamount(grosspay, taxamount, taxrate, n);
	findnetpay(grosspay, netpay, taxamount, totalnetpay, n);
	findsum(sum, netpay, n);
	findavg(average, sum, totalnetpay, n);
	printalldata(employeeid, grosspay, taxrate, netpay, otpay, n);

		cout << endl << "The average net pay is " << setprecision(1) << average << endl;
	}//MAIN

int readalldata(long int employeeid[], int hworked[], float hrate[], int n){
		ifstream fin("Payroll.txt");
		n = 0;

		while (fin >> employeeid[n] >> hworked[n] >> hrate[n]) n++;

		fin.close();
		return n;
}//READALLDATA

void findothours(int hworked[], int othours[], int n){
	for (int i = 0; i < n; i++){
		if (hworked[i]>40)othours[i] = hworked[i] - 40;
		else othours[i] = 0;
	}//FOR
}//FINDOTHOURS

void findotpay(int othours[], float hrate[], float otpay[], int n){
	for (int i = 0; i < n; i++){
		otpay[i] = othours[i] * (hrate[i] * 1.5);
	}//FOR
}//FINDOTPAY

void findreghours(int hworked[], int reghours[], int n){
	for (int i = 0; i < n; i++){
		if (hworked[i]>40)reghours[i] = 40;
		else reghours[i] = hworked[i];
	}//FOR
}//FINDREGHOURS

void findregpay(int reghours[], float regpay[], float hrate[], int n){
	for (int i = 0; i < n; i++){
		regpay[i] = reghours[i] * hrate[i];
	}//FOR
}//FINDREGPAY

void findgrosspay(float regpay[], float otpay[], float grosspay[], int n){
	for (int i = 0; i < n; i++){
		grosspay[i] = regpay[i] + otpay[i];
	}//FOR
}//FINDGROSSPAY

void findtaxrate(float grosspay[], float taxrate[], int n){
	for (int i = 0; i < n; i++){
		if (grosspay[i]>1000)   taxrate[i] = 0.30;
		else if (grosspay[i] > 800)   taxrate[i] = 0.20;
		else if (grosspay[i] > 500)   taxrate[i] = 0.10;
		else taxrate[i] = 0.0;
	}//FOR
}//FINDTAXRATE

void findtaxamount(float grosspay[], float taxamount[], float taxrate[], int n){
	for (int i = 0; i < n; i++){
		taxamount[i] = grosspay[i] * taxrate[i];
	}//FOR
}//FINDTAXAMOUNT

void findnetpay(float grosspay[], float netpay[], float taxamount[], float totalnetpay, int n){
	for (int i = 0; i < n; i++){
		netpay[i] = grosspay[i] - taxamount[i];
		//totalnetpay = totalnetpay + netpay[i];
	}//FOR
}//FINDNETPAY

void findsum(float sum, float netpay[], int n){
	for (int i = 0; i < n; i++){
		sum+= netpay[i];
	}//FOR
}//FINDSUM

void findavg(float average, float sum, float totalnetpay, int n){
	for (int i = 0; i < n; i++){
		average = sum/i;
	}
}//FINDAVERAGE


void printalldata(long int employeeid[], float grosspay[], float taxrate[], float netpay[], float otpay[],  int n){
	cout << left << setw(8) << "EMP ID" << setw(11) << "GROSS PAY" << setw(11) << "TAX RATE" << setw(10) << "NET PAY" << setw(15) << "OVERTIME PAY" << endl;

	for (int i = 0; i < n; i++){
		cout << left << setw(8) << employeeid[i] << setw(11) << grosspay[i] << setw(11) << taxrate[i] << setw(10) << netpay[i] << setw(15) << otpay[i] << endl;
	}//FOR
}//PRINTALLDATA 
blimey that's a lot of arrays and some dense code :)

- are you learning from an old book? it main should return int and not void.
- i think this is your issue:
1
2
3
4
5
6
7
void findavg(float average, float sum, float totalnetpay, int n){
	for (int i = 0; i < n; i++)
        {
		average = sum/i;
	}
}//FINDAVERAGE


You are passing in a float to represent your average. are you aware that updating the variable in this function will NOT be reflected in your average value in your main funtion? This is because you are passing by value. either make the function return your average (see below) or pass by reference (http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/)

This appears to be your problem in a lot of your functions. If you have a function called "findSum()", would it not be sensible to return this sum back to main? e.g. change this:

1
2
3
4
5
void findsum(float sum, float netpay[], int n){
	for (int i = 0; i < n; i++){
		sum+= netpay[i];
	}//FOR
}//FINDSUM 


to something like this:
1
2
3
4
5
6
7
8
9
float findsum(float netpay[], int n)
{
        float sum = 0.0;
	for (int i = 0; i < n; i++)
       {
		sum+= netpay[i];
	}
       return sum;
}

maybe?
Last edited on
On line 112 you have commented out the accumulator for totalnetpay.

Also note the difference:
1
2
3
4
5
6

void findsum(float sum, float netpay[], int n) // changes  to sum will not be reflected outside the function

float findsum(netpay[], int n) // function can return float sum

float findsum(float &sum, float netpay[], int n) // changes to sum will be reflected outside the function 
Topic archived. No new replies allowed.