# help with accumulating in payroll program

• 10-14-2002
indigo0086
help with accumulating in payroll program
Code:

////////////////////////////////////////////////////
// Daniel Pritchett
// COP1220
// Payroll Program made to generate
// payroll report based on employee information
////////////////////////////////////////////////////

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;
double overtimeGross(int, double, double);
double federalTax(int, double);

int main()
{
string EmployeeFirstName, EmployeeLastName, EmployeeNumber;
double HourlyRate, GrossPay, RegPay, FedTax, NetPay, Overtime;
double Total_RegPay, Total_Overtime, Total_GrossPay, Total_FedTax, Total_NetPay;
ofstream outFile;

outFile.open("a:test.txt");
outFile << fixed;
outFile <<"EMPLOYEEE" << setw(16) << "EMPLOYEE" << setw(10) << "REG" << setw(16)
<< "OVERTIME" << setw(14) << "GROSS" << setw(9) << "FED"
<< setw(11) << "NET" <<endl;
outFile <<"NUMBER" << setw(15) << "NAME" << setw(14) << "PAY"
<< setw(11) << "PAY"
<< setw(17) << "PAY" << setw(11) << "TAX" << setw(11) << "PAY" << endl << endl;

cout << "How many employees are you going to enter in the payroll? ";
cin >> Workers;

//saves worker number for use in the end.

Total_RegPay = 0;
Total_Overtime = 0;
Total_GrossPay = 0;
Total_FedTax = 0;
Total_NetPay = 0;
//for loop runs as long as Workers does not equal 0
for( ; Workers!=0 ; Workers--)
{

cout << "\nEnter the Employee's six digit work Number: ";
cin >> EmployeeNumber;
cout << "Enter the employees first and last name: ";
cin >> EmployeeFirstName >> EmployeeLastName;
cout << "Enter the number of Dependents: ";
cin >> Dependents;
cout << "Enter your Hourly Rate: \$";
cin >> HourlyRate;
cout << "Enter the numbers of hours woked: ";
cin >> HoursWorked;
RegPay = HourlyRate * HoursWorked;

//goes into outfile, change cout to outFile
outFile << right << EmployeeNumber
<< setw(9) << EmployeeFirstName<< " " << EmployeeLastName
<< setw(6) << right << setprecision(2)<< "\$" << RegPay;

//condition for overtime
if(HoursWorked > 40)
{
Overtime =  overtimeGross(HoursWorked, HourlyRate, RegPay);
outFile << setw(9) << right << setprecision(2)<< "\$" << Overtime;
GrossPay = Overtime + RegPay;

}
else
{
Overtime = 0;
GrossPay = RegPay;
outFile << setw(12) << right <<setprecision(2)
<< "\$" << Overtime;

}

outFile << setw(9) << right << "\$" << setprecision(2) << GrossPay;

//calculating federal tax
FedTax = federalTax(Dependents, GrossPay);
outFile << setw(5) << right << "\$" << setprecision(2) << FedTax;

//calculating net pay
NetPay = GrossPay - FedTax;
outFile << setw(5) <<right << "\$" << setprecision(2) << NetPay << endl;

Total_RegPay += RegPay;
Total_Overtime += Overtime;
Total_GrossPay += GrossPay;
Total_FedTax += FedTax;
Total_NetPay += NetPay;

//end of for loop
}

outFile <<"\n\n"<< setw(25) <<setprecision(2)<< "TOTAL:" << setw(6) << "\$"
<< Total_RegPay << setw(8) << "\$" << Total_Overtime << setw(10)
<< "\$"
<< Total_GrossPay << setw(6) << "\$" << Total_FedTax << setw(7) << "\$" <<  Total_NetPay;
return 0;
}

double overtimeGross(int HoursWorked, double HourlyRate, double RegPay)
{
return (HoursWorked - 40) * (HourlyRate * 1.5);
}

double federalTax(int Dependents, double GrossPay)
{
return (GrossPay - (Dependents * 42.15)) * .20;

}

see inquiry belowat bottom
• 10-14-2002
indigo0086
the only thing is, before I do the
Total_X += Total_X;
I have to assighn total_x to the first gross amount, but then it wouldnt' really work because everytime it loops, it will just re-assign the value of the Total_X. You understand?
• 10-14-2002
Hammer
Quote:

Originally posted by indigo0086
the only thing is, before I do the
Total_X += Total_X;
I have to assighn total_x to the first gross amount, but then it wouldnt' really work because everytime it loops, it will just re-assign the value of the Total_X. You understand?

Well, if you initalise Total_X to 0 before the loop, then use something like this:
>Total_X += OnePersons_X;
... it will added up as you go through the loop, then you just cout Total_X at the end.
• 10-14-2002
indigo0086
oh I get you so it would be something like
Code:

//for loop
{

OneTotalGross = Gross_Pay
Total_Gross += OneTotalGross
}

• 10-14-2002
Hammer
Yeah, something like that. See what you come up with. :)
• 10-15-2002
indigo0086
okay, above is my newly made. Everything is set exept the output has to be formatted so that they are evenly columned. How would What is the best way using basic formatting methods to ouput the format in even columns. Right Justified btw.
• 10-15-2002
indigo0086
EUREKA! I finally finished it. SOrry for the messy code but that's what I was working with. I just wish there was an easier way for me to output information other than trial and error. Doh, forgot to mention thankx hammer for your help. Now I have to write the pseudocode for the prog cuz our teacher requires it ;(