Expanding the existing payroll program

I have working on the the expansion of a payroll system for a project for about a month now ad I just recent ran into a some trouble.

The program in is working, but I am having difficulty trying to get Calculate the averages of information in the program.

What I am trying to do is Calculate the average of all employee net pays (salary) and I am also trying to Display the computations and average of at least 5 employees.

Here is the program that I have so far:


#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
class payroll{
ifstream fin;
int n;
char employeeid[20];
char employeename[20];
int hoursworked,overtime;
char maritalstatus;
double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay;
double totalnetpay,avgnetpay;
void calculategrosspay();
void calculatetax();
void calculatenetpay();
//void findavgnetpay();
void printheadings();
void printdata();
public:payroll();
~payroll();
void printreport(); };
payroll::payroll(){
fin.open("payroll.txt"); }//CONSTRUCTOR
payroll::~payroll(){
fin.close(); }//DESTRUCTOR
void payroll::calculategrosspay(){
if(hoursworked > 40){
overtime=hoursworked-40;
regularpay=hoursworked*hourlyrate;
overtimepay=overtime*(hourlyrate*1.5);
grosspay=regularpay+overtimepay; }//IF
else{ grosspay=hoursworked*hourlyrate;
regularpay=grosspay;
overtimepay=0;}//ELSE
}//CALCULATEGROSSPAY
void payroll::calculatetax(){
if(grosspay >=500)taxrate=.30;
else if(grosspay>200.00)taxrate = .20;
else taxrate =.10;
if(maritalstatus=='S'||maritalstatus=='S')
taxrate = taxrate + .05;
taxamount=grosspay*taxrate; }//CALCULATETAX
void payroll::calculatenetpay(){
netpay=grosspay-taxamount;
totalnetpay=totalnetpay+netpay;
}//CALCULATENETPAY
//avgnetpay=totalnetpay;
//}//FINDAVGNETPAY
void payroll::printheadings(){
cout<<setw(45)<<"-Dr Arkale's 'Payroll program-"<<endl;
cout<<"---------------------------------------------------------"<<endl;
cout<<" NAME ID HW HR RT OT-PAY GROSS"
" TAX NETPAY"<<endl;
cout<<"---------------------------------------------------------"<<endl;
}//PRINTHEADINGS
void payroll::printdata(){
cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
cout<<setw(6)<<employeename<<setw(12)<<employeeid<<setw(4)
<<hoursworked<<setw(3)<<overtime<<setw(8)<<regularpay<<setw(8)
<<overtimepay<<setw(8)<<grosspay<<setw(8)<<taxamount<<setw(8)
<<netpay<<endl; }//PRINTDATA
void payroll::printreport(){
n=0; totalnetpay=0;
printheadings();
while(fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate){
calculategrosspay();
calculatetax();
calculatenetpay();
printdata();
n++; }//WHILE

//findavgnetpay();
//cout<<endl<<"The average net pay for "<<n<<" employees is "<<avgnetpay<<endl;

}//PRINTREPORT


int main(){
payroll employee;
employee.printreport();
//system ("pause");
}//MAIN


Here is the output for from the program.


-Dr Arkale's 'Payroll program-
---------------------------------------------------------
NAME ID HW HR RT OT-PAY GROSS TAX NETPAY
---------------------------------------------------------
Sadeek 176-76-5679 39 3950.00 2925.00 6875.00 2062.50 4812.50
Mellisa 078-56-7654 71 31 3195.00 2092.50 5287.50 1586.25 3701.25
Jody 065-67-8951 54 14 2700.00 1050.00 3750.00 1312.50 2437.50
Amalik 097-45-4567 79 39 3950.00 2925.00 6875.00 2062.50 4812.50
Jessia 124-56-7865 78 38 3588.00 2622.00 6210.00 1863.00 4347.00

--------------------------------
Process exited after 0.01909 seconds with return value 0
Press any key to continue . . .


I appreciate the help, thanks!!
Your code is pretty much unreadable without reasonable indentation and code tags.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

What does an instance of a payroll object represent? An employee? Or a payroll system?
You seem to be mixing the two. findavgnetpay() is not going to work the way you want. Where you have the call to findavgnetpay(), you've already passed over all the employees in the file.

I strongly recommend that you implement payroll and employee in separate classes.

You can do what you want after calculatenetpay() by adding each employee's net pay to totalnetpay. Then after the while, compute avgnetpay by dividing totalnetpay by the number of employees.

Last edited on
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

#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
class payroll{
ifstream fin;
int n;
char employeeid[20];
char employeename[20];
int hoursworked,overtime;
char maritalstatus;
double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay;
double totalnetpay,avgnetpay;
void calculategrosspay();
void calculatetax();
void calculatenetpay();
//void findavgnetpay();
void printheadings();
void printdata();
public:payroll();
~payroll();
void printreport(); };
payroll::payroll(){
fin.open("payroll.txt"); }//CONSTRUCTOR
payroll::~payroll(){
fin.close(); }//DESTRUCTOR
void payroll::calculategrosspay(){
if(hoursworked > 40){
overtime=hoursworked-40;
regularpay=hoursworked*hourlyrate;
overtimepay=overtime*(hourlyrate*1.5);
grosspay=regularpay+overtimepay; }//IF
else{ grosspay=hoursworked*hourlyrate; 
regularpay=grosspay; 
overtimepay=0;}//ELSE
}//CALCULATEGROSSPAY
void payroll::calculatetax(){
if(grosspay >=500)taxrate=.30;
else if(grosspay>200.00)taxrate = .20;
else taxrate =.10;
if(maritalstatus=='S'||maritalstatus=='S')
taxrate = taxrate + .05;
taxamount=grosspay*taxrate; }//CALCULATETAX
void payroll::calculatenetpay(){
netpay=grosspay-taxamount;
totalnetpay=totalnetpay+netpay;
}//CALCULATENETPAY
//avgnetpay=totalnetpay;
//}//FINDAVGNETPAY 
void payroll::printheadings(){
cout<<setw(45)<<"-Dr Arkale's 'Payroll program-"<<endl;
cout<<"---------------------------------------------------------"<<endl;
cout<<" NAME ID HW HR RT OT-PAY GROSS"
" TAX NETPAY"<<endl;
cout<<"---------------------------------------------------------"<<endl;
}//PRINTHEADINGS
void payroll::printdata(){
cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
cout<<setw(6)<<employeename<<setw(12)<<employeeid<<setw(4)
<<hoursworked<<setw(3)<<overtime<<setw(8)<<regularpay<<setw(8)
<<overtimepay<<setw(8)<<grosspay<<setw(8)<<taxamount<<setw(8)
<<netpay<<endl; }//PRINTDATA
void payroll::printreport(){
n=0; totalnetpay=0;
printheadings();
while(fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate){
calculategrosspay();
calculatetax();
calculatenetpay();
printdata();
n++; }//WHILE

//findavgnetpay();
//cout<<endl<<"The average net pay for "<<n<<" employees is "<<avgnetpay<<endl;

}//PRINTREPORT


int main(){
payroll employee;
employee.printreport();
//system ("pause");
}//MAIN



Topic archived. No new replies allowed.