Payroll Calculation Error?

I'm working on a payroll program and I'm having problems that are causing calculation errors.
_____________________________________________________________________________
My input file is
1234 Fred Jones 40 25.00
2345 Terry Smith 60 10.00
_____________________________________________________________________________
The errors I get are from the regular pay, over time pay, gross pay, tax amount
and net pay calculations. They all look similar to this (6e+002). I believe they are occurring because regular pay isn't calculating correctly and the rest depend on that for their calculations. However, I can't see why regular pay isn't calculating correctly. Anyone see what I'm missing?
_____________________________________________________________________________
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
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
class payroll{
	ifstream fin;
	char employeeid[100];
	char employeefname[100];
	char employeelname[100];
	int hoursworked, overtimehours;
	double hourlyrate, overtimepay, regularpay, grosspay, taxrate, taxamount, netpay;
	void calculategrosspay();
	void calculatetax();
	void calculatenetpay();
	void printheadings();
	void printdata();
public:payroll();
	   ~payroll();
	   void printreport();  };
payroll::payroll(){
	fin.open("payroll.txt"); }//CONSTRUCTOR
	payroll::~payroll(){
		fin.close();
	}//DECONSTRUCTOR
	void payroll::calculategrosspay(){
		if (hoursworked > 40){
			overtimehours = hoursworked - 40;
			regularpay = hoursworked * hourlyrate;
			overtimepay = overtimehours * (hourlyrate * 1.5);
			grosspay = regularpay + overtimepay;}//IF
		else grosspay = hoursworked * hourlyrate;}//CALCULATEGROSSPAY
	void payroll::calculatetax(){
		taxrate = .30;
		taxamount = grosspay * taxrate;
	}//CALCULATE TAX
	void payroll::calculatenetpay(){
		netpay = grosspay - taxamount;
	}//Calculate Net Pay
	void payroll::printheadings(){
		cout << setw(67) << "-SMALL BUSINESS PAYROLL REPORT-" << endl;
		cout << "-------------------------------------------------------------------" << endl;
		cout << "EMPLOYEE ID" << setw(12)
			<< "FIRST NAME" << setw(12)
			<< "LAST NAME" << setw(8)
			<< "HW" << setw(8)
			<< "HR" << setw(8)
			<< "OTH" << setw(8)
			<< "RP" << setw(8)
			<< "OTP" << setw(8)
			<< "GP" << setw(12)
			<< "TAX AMT" << setw(10)
			<< "NET PAY" << endl;
		cout << "---------------------------------------------------------------------" << endl;
	}//PRINT HEADINGS
	void payroll::printdata(){
		cout << setprecision(2);
		cout << employeeid << setw(13)
			<< employeefname << setw(14)
			<< employeelname << setw(12)
			<< hoursworked << setw(8)
			<< hourlyrate << setw(6)
			<< overtimehours << setw(8)
			<< regularpay << setw(8)
			<< overtimepay << setw(12)
			<< grosspay << setw(10)
			<< taxamount << setw(10)
			<< netpay << endl; }//PRINT ALL DATA
	void payroll::printreport(){
		int i = 0;
		printheadings();
		while (fin >> employeeid >> employeefname >> employeelname >> hoursworked >> hourlyrate){
			calculategrosspay();
			calculatetax();
			calculatenetpay();
			printdata();
			i++;
		}//WHILE
	}//PRINTREPORT
	void main(){
		payroll employee;
		employee.printreport();
		system("pause");
	}//MAIN 
closed account (j3Rz8vqX)
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    void payroll::calculategrosspay(){
        if (hoursworked > 40){
            overtimehours = hoursworked - 40;
            regularpay = 40 * hourlyrate;//Modified to be 40 flat for max regular hours;
            overtimepay = overtimehours * (hourlyrate * 1.5);
            grosspay = regularpay + overtimepay;
        }//IF
        else
        {
            overtimehours= 0;//Implemented to ensure default value;
            regularpay = hoursworked * hourlyrate;
            overtimepay = 0;//Implemented to ensure default value;
            grosspay = regularpay;//gross pay = regular pay?
        }
    }//CALCULATEGROSSPAY
                                    -SMALL BUSINESS PAYROLL REPORT-
-------------------------------------------------------------------
EMPLOYEE ID  FIRST NAME   LAST NAME      HW      HR     OTH      RP     OTP
 GP     TAX AMT   NET PAY
---------------------------------------------------------------------
1234         Fred         Jones          40      25     0  1e+003       0      1
e+003    3e+002    7e+002
2345        Terry         Smith          60      10    20  4e+002  3e+002      7
e+002  2.1e+002  4.9e+002


Regular pay for Fred: 1e+003 == 1000 == 40*25;
Regular pay for Terry: 4e+002 == 400 == 400 == 10*40

Edited to provide some details...

Edit, it looks correct to me, maybe you aren't sure what you should be expecting.
Last edited on
hmmm...tried adding that and it changed basically nothing I still have error numbers in all the same spots the errors are just different now. 4e+002 for example rather then the 6e+002. I know it's a calculation error but I'll got no idea why it's happening at the moment.
closed account (j3Rz8vqX)
4e+002 is correct for RP - the regular pay without over time.

He had 300 dollars extra from over time, shown in gross pay.

40(max regular)*10= 400! correct.
20 * 10(1.5 : overtime) = 300 + 400(RP) = 7e+002(GP)

=D
Ok the output you added above are what my values look like but how do I get them to express as numeric values instead?

Edit: well by that i mean values like 300 and 400.
Last edited on
closed account (j3Rz8vqX)
If I had said fixed, it would have been confusing, so here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
	void payroll::printdata(){
		cout << setprecision(2);
		cout << employeeid << setw(13)
			<< employeefname << setw(14)
			<< employeelname << setw(12)
			<< hoursworked << setw(8)
			<< hourlyrate << setw(6)
			<< overtimehours << setw(8) << fixed
			<< regularpay << setw(8) << fixed
			<< overtimepay << setw(12) << fixed
			<< grosspay << setw(10) << fixed
			<< taxamount << setw(10) << fixed
			<< netpay << endl; }//PRINT ALL DATA
                                    -SMALL BUSINESS PAYROLL REPORT-
-------------------------------------------------------------------
EMPLOYEE ID  FIRST NAME   LAST NAME      HW      HR     OTH      RP     OTP
 GP     TAX AMT   NET PAY
---------------------------------------------------------------------
1234         Fred         Jones          40      25     0 1000.00    0.00     10
00.00    300.00    700.00
2345        Terry         Smith          60   10.00    20  400.00  300.00      7
00.00    210.00    490.00

Edited: added output.
Last edited on
Yeah your explanation is very helpful and I understand that things like 4e+002 represent/link different values thanks to you (which is a good learning experience in itself.) I just still have to figure out how to get them back to looking like 400.00, 300.00 and 1000.00. Then I have some more things to do with the program before it's finished.

:)

I'm not much of a programer and some of the things people on this forum help out with are really amazing. I hope it gets easier with practice because right now it's more long hours and headaches.
closed account (j3Rz8vqX)
<< fixed before the data will set the output to your liking; 400.00.

Example:<< setw(8) << fixed << regularpay //stuff

After using code, a dozen or so times, it'll become habitual; second nature.

Tip:
Focus on design, not syntax, and refer to online documentations or your text books for syntax when needed. Slowly decrease the number of reference to syntax, that you use a lot, and you'll eventually find yourself remembering small pieces of code.

When under deadlines and stress, you can find yourself using code.
When not, you'll find yourself learning code.

Have a good day.
Thanks! with the <<fixed added to it's back to regular numerical values!

I think I've got this program just about wrapped up but I have one more calculation related error going on. This time it's related to finding the net pay average for all the employee's in my input file.

I think I have this just about worked out but I'm getting an error that looks like this

1 error C4716: 'payroll::findavg' : must return a value

Here is what my program looks like now.

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
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
class payroll{
	ifstream fin;
	char employeeid[100];
	char employeefname[100];
	char employeelname[100];
	int hoursworked, overtimehours;
	double hourlyrate, overtimepay, regularpay, grosspay, taxrate, taxamount, netpay, sum, average;
	void calculategrosspay();
	void calculatetax();
	void calculatenetpay();
	void printheadings();
	void printdata();
	void findsum(int);
	double findavg(double, int);
public:payroll();
	   ~payroll();
	   void printreport();  };
payroll::payroll(){
	fin.open("payroll.txt"); }//CONSTRUCTOR
	payroll::~payroll(){
		fin.close();
	}//DECONSTRUCTOR
	void payroll::calculategrosspay(){
		if (hoursworked > 40){
			overtimehours = hoursworked - 40;
			regularpay = 40 * hourlyrate;
			overtimepay = overtimehours * (hourlyrate * 1.5);
			grosspay = regularpay + overtimepay;
		}//IF
		else
		{
			overtimehours = 0;
			regularpay = hoursworked * hourlyrate;
			overtimepay = 0;
			grosspay = regularpay;
		}
	}//CALCULATEGROSSPAY 
	void payroll::calculatetax(){
		taxrate = .30;
		taxamount = grosspay * taxrate;
	}//CALCULATE TAX
	void payroll::calculatenetpay(){
		netpay = grosspay - taxamount;
	}//Calculate Net Pay
	void payroll::printheadings(){
		cout << setw(67) << "-SMALL BUSINESS PAYROLL REPORT-" << endl;
		cout << "--------------------------------------------------------------" << endl;
		cout << "EMPLOYEE ID" << setw(12)
			<< "FIRST NAME" << setw(12)
			<< "LAST NAME" << setw(6)
			<< "HW" << setw(8)
			<< "HR" << setw(8)
			<< "OTH" << setw(10)
			<< "OTP" << setw(8)
			<< "GP" << setw(12)
			<< "TAX AMT" << setw(10)
			<< "NET PAY" << endl;
		cout << "--------------------------------------------------------------" << endl;
	}//PRINT HEADINGS
	void payroll::printdata(){
		cout << fixed << setprecision(2);
		cout << employeeid << setw(13)
			<< employeefname << setw(14)
			<< employeelname << setw(10)
			<< hoursworked << setw(9)
			<< hourlyrate << setw(6)
			<< overtimehours << setw(11)
			<< overtimepay << setw(10)
			<< grosspay << setw(9)
			<< taxamount << setw(10)
			<< netpay << endl; }//PRINT ALL DATA

	void payroll::findsum(int i){
		sum += netpay;
	}//FIND SUM
	double payroll::findavg(double sum, int i){
		average = sum / i;
		cout << endl << "The netpay average is " << average << endl;
	}//FIND AVERAGE

	void payroll::printreport(){
		int i = 0;
		printheadings();
		while (fin >> employeeid >> employeefname >> employeelname >> hoursworked >> hourlyrate){
			calculategrosspay();
			calculatetax();
			calculatenetpay();
			printdata();
			i++;
			findsum(i);
		}//WHILE
		findavg(sum, i);
	}//PRINTREPORT
	void main(){
		payroll employee;
		employee.printreport();
		system("pause");
	}//MAIN 
You've defined payroll::findavg to return a double:

double findavg(double, int);

but nowhere in the method definition do you actually return anything:

80
81
82
83
	double payroll::findavg(double sum, int i){
		average = sum / i;
		cout << endl << "The netpay average is " << average << endl;
	}//FIND AVERAGE 
Ok so I can wrap my head around the idea that the error is occurring because I haven't constructed my means of finding the average correctly. Can you give me any advice on how I can go about returning a value so that it calculates correctly?

Can I change/add to the code to make it work and calculate?
1
2
3
4
double payroll::findavg(double sum, int i){
		average = sum / i;
		cout << endl << "The netpay average is " << average << endl;
	}//FIND AVERAGE 

Your code is calculating the average just fine (as far as I can see). It's just that your method isn't returning anything.
I guess I'm not seeing how I should be retuning what I need to be returning.

So I guess the makes my question is how do I return a result from the above lines of code that we've been discussing?

Sorry if I'm being unclear.
Last edited on
You'd put a return statement at the end of the calculation.

1
2
3
4
5
double payroll::findavg(double sum, int i){
		average = sum / i;
		cout << endl << "The netpay average is " << average << endl;
		return average; // <- return statement here
	}//FIND AVERAGE  


The calling function would then need a way to hold the value returned from the function if you were going to use it again.
avg = findavg(sum, i);

But since this function prints out the average - do you need the function to return the value of the average or can it be another void function?


Also...
void main() should be int main()
ha thanks I've been fooling around with this for awhile now and I added the line return average just before checking back here to see your post wildblue! Sometimes I seem to miss the forest for the trees. It was a pretty simple fix when I was thinking it was something else.

Now the program is up and running as intended!
Last edited on
Topic archived. No new replies allowed.