sum for -should be easy but im stuck

Hi all,

I have been working on trying to sum the netpays so that I can get an average as well. I think the issue is within the while loop I have created. My cout for the value "sum" just places the last inputed netpay number instead of actually adding all the netpays together. I have this in an array that will only accept two employees for now, I will tweak that part latter. Any help here would be greatly appreiciated!!
Thanks!

#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>


int main(){




cout <<" Creating Array-It is set only to enter 2 employess"<<endl;
int employeeID[2],numberofEMPLOYEES, NUMofEmployees, sum, A, B,i,average;
float hourlyRATE[2], grossPAY, taxAMOUNT,netPAY[2] ;
float TAXRATE;
char maritalstatus[2], firstName[2][10], lastName[2][15];
float overTIME, overTIMEPAY, regularPAY, hoursWORKED[2];
float timePLUSHALF, maritaltax;

regularPAY=0;
overTIMEPAY=0;
overTIME=0;
A=0;
B=0;
i=0;
sum=0;

while(firstName[A]&&A<2)//less than 2 so that it outputs data after 2 inputs
{
cout<<"Enter First Name:";
cin>>firstName[A];
cout<<"Enter Last Name:";
cin>>lastName[A];
cout<<"Enter S for Single, Enter H for Head of Household, Enter M for Married: ";
cin>>maritalstatus[A];
cout<<"Enter Employee ID:";
cin>>employeeID[A];
cout<<"Enter the Hours Worked:";
cin>>hoursWORKED[A];
cout<<"Enter the Hourly Rate:";
cin>>hourlyRATE[A];

A=A+1;

}// while loop #1 closed

cout<<" TEST PAYROLL INSTITUTE"<<endl;

cout<<"First Name Last Name Status ID RegHrs Rate OT OTPay Gross Tax NET"<<endl;
cout<<"========== ========= ====== ==== ====== ==== ==== ===== ===== === === "<<endl<<endl;





while(B<A)
{
grossPAY=hoursWORKED[B]*hourlyRATE[B];




if(hoursWORKED[2]>40)
{
overTIME=hoursWORKED[B]-40;
regularPAY=40*hourlyRATE[B];
timePLUSHALF=hourlyRATE[B]*1.5;
overTIMEPAY=timePLUSHALF*overTIME;
grossPAY=overTIMEPAY+regularPAY;

}




if(grossPAY>1000)
TAXRATE=.30;
else if(grossPAY>800)
TAXRATE=.20;

else if (grossPAY>500)
TAXRATE=.10;
else if(grossPAY<500)
TAXRATE=0;

if(maritalstatus[B]=='S')
maritaltax=TAXRATE*1.05;
else if(maritalstatus[B]=='H')
maritaltax=TAXRATE*.95;
else if(maritalstatus[B]=='M')
maritaltax=0;





taxAMOUNT=TAXRATE*grossPAY;


netPAY[i]=grossPAY-taxAMOUNT;


average= sum/2;




setprecision(2);

cout<<setiosflags(ios::left)<<setw(11)<<firstName[B];
cout<<setiosflags(ios::left)<<setw(10)<<lastName[B];
cout<<setiosflags(ios::left)<<setw(7)<<maritalstatus[B];
cout<<setiosflags(ios::left)<<setw(5)<<employeeID[B];
cout<<setiosflags(ios::left)<<setw(7)<<hoursWORKED[B];
cout<<setiosflags(ios::left)<<setw(5)<<hourlyRATE[B];
cout<<setiosflags(ios::left)<<setw(5)<<overTIME;
cout<<setiosflags(ios::left)<<setw(6)<<overTIMEPAY;
cout<<setiosflags(ios::left)<<setw(7)<<grossPAY;
cout<<setiosflags(ios::left)<<setw(7)<<taxAMOUNT;
cout<<setiosflags(ios::left)<<setw(6)<<netPAY[i]<<endl;

B=B+1;
}//while loop #2 closed

while(netPAY[i]<2); //taking netPay[i] where netPAY[i] gets stored in the array??
{
sum=sum + netPAY[i];
i=i+1;;
}//close loop for sum of all netpays*/


cout<<"Total Netpay is: "<<sum<<endl;
cout<<"Average Netpay is: "<<average<<endl;

system("PAUSE");

return 0;

}//MAIN
Last edited on
I think somewhere you need sum+=netPay; in the second while loop.
It would be clearer if you used code tags.
There is a much easier way to write this program. I just changed around a few things. Needs much more fixing but Im tired

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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main(){


cout <<" Creating Array-It is set only to enter 2 employess"<<endl;
int employeeID[2],numberofEMPLOYEES, NUMofEmployees, sum, A, B,i,average;
float hourlyRATE[2], grossPAY, taxAMOUNT,netPAY[2] ; 
float TAXRATE;
char maritalstatus[2], firstName[2][10], lastName[2][15];
float overTIME, overTIMEPAY, regularPAY, hoursWORKED[2];
float timePLUSHALF, maritaltax;

regularPAY=0;
overTIMEPAY=0;
overTIME=0;
A=0;
B=0; 
i=0; 
sum=0;

for(int j=0; j<1; j++) //Use a for loop if you know exactly how many times to loop
{ 
cout<<"Enter First Name:";
cin>>firstName[j];
cout<<"Enter Last Name:";
cin>>lastName[j];
cout<<"Enter S for Single, Enter H for Head of Household, Enter M for Married: ";
cin>>maritalstatus[j];
cout<<"Enter Employee ID:";
cin>>employeeID[j];
cout<<"Enter the Hours Worked:";
cin>>hoursWORKED[j];
cout<<"Enter the Hourly Rate:";
cin>>hourlyRATE[j];
}

cout<<" TEST PAYROLL INSTITUTE"<<endl<< endl;


for(int j=0; j<1; j++) //You will have decide on a set number of entries or a way to identify the end of the array.
{
grossPAY=hoursWORKED[j]*hourlyRATE[j];



if(hoursWORKED[j]>40)
{
overTIME=hoursWORKED[j]-40;
regularPAY=40*hourlyRATE[j];
timePLUSHALF=hourlyRATE[j]*1.5;
overTIMEPAY=timePLUSHALF*overTIME;
grossPAY=overTIMEPAY+regularPAY;

}




if(grossPAY>1000)
TAXRATE=.30;
else if(grossPAY>800)
TAXRATE=.20;

else if (grossPAY>500)
TAXRATE=.10;
else if(grossPAY<500)
TAXRATE=0;

if(maritalstatus[j]=='S')
maritaltax=TAXRATE*1.05;
else if(maritalstatus[j]=='H')
maritaltax=TAXRATE*.95;
else if(maritalstatus[j]=='M')
maritaltax=0;





taxAMOUNT=TAXRATE*grossPAY;


netPAY[j]=grossPAY-taxAMOUNT;


setprecision(2);

cout<<"FIRST NAME " << setiosflags(ios::left)<<setw(11)<<firstName[j]<< endl;
cout<< "LAST NAME " << setiosflags(ios::left)<<setw(10)<<lastName[j]<< endl;
cout<< "M-STATUS " << setiosflags(ios::left)<<setw(7)<<maritalstatus[j]<< endl;
cout<<"ID " << setiosflags(ios::left)<<setw(5)<<employeeID[j]<< endl;
cout<< "TOT-HOURS " << setiosflags(ios::left)<<setw(7)<<hoursWORKED[j]<< endl;
cout<< "HR-RATE " << setiosflags(ios::left)<<setw(5)<<hourlyRATE[j]<< endl;
cout<< "OT " << setiosflags(ios::left)<<setw(5)<<overTIME<< endl;
cout<< "OT-PAY " << setiosflags(ios::left)<<setw(6)<<overTIMEPAY<< endl;
cout<< "GROSS PAY " << setiosflags(ios::left)<<setw(7)<<grossPAY<< endl;
cout<< "TAX AMNT " << setiosflags(ios::left)<<setw(7)<<taxAMOUNT<< endl;
cout<< "NET PAY " << setiosflags(ios::left)<<setw(6)<<netPAY[j]<<endl;




//What is netpay? Gross - Taxes?

cout<<"Total Netpay is: "<< netPAY[j]<<endl;

}//End of for loop
system("PAUSE"); 

return 0;

}//MAIN 


Maybe you don't want for loops if you want any amount of entries. You can change it back easily enough. Either way, do yourself a favor and use more descriptive variables.
Last edited on
Thanks everyone for the help. As you can tell I am new at this. Thanks for pointing things out for me RKA. I will try it!!
~M
Topic archived. No new replies allowed.