My program figures the payroll details for two employees and then calculates a total. My problem is in rounding. My results are off by a penny. I need to know how to round a variable so that I can use it in an addition statment. Here is my code.
/* Lab 03P2.cpp
Name: Melissa Beyer
Date: 09/09/01
Purpose: Calculate Payroll
Input: Hours worked and Hourly rate for two employees.
Output: Employee number, Hours worked, hourly rate, gross pay, tax, Net pay. */
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double TaxRate = .2; //Tax rate as decimal 0.2 = 20%.
double Worked1, PayRate1, Gross1, Tax1, Net1;
double Worked2, PayRate2, Gross2, Tax2, Net2;
double WorkedTot, GrossTot, TaxTot, NetTot;
cout << "CPT 267 Lab 03P2 - Melissa Beyer" << endl;
cout << "Program to calculate payroll." << endl << endl;
//Get info for Employee #1.
cout << "Enter hourly rate for Employee #1: ";
cin >> PayRate1;
cout << endl;
cout << "Enter number of hours worked for Employee #1: ";
cin >> Worked1;
cout << endl << endl;
//Get info for Employee #2.
cout << "Enter hourly rate for Employee #2: ";
cin >> PayRate2;
cout << endl;
cout << "Enter number of hours worked for Employee #2: ";
cin >> Worked2;
cout << endl << endl;
//Calculations for Employee #1.
Gross1 = PayRate1 * Worked1;
Tax1 = Gross1 * TaxRate;
Net1 = Gross1 - Tax1;
//Calculations for Employee #2.
Gross2 = PayRate2 * Worked2;
Tax2 = Gross2 * TaxRate;
Net2 = Gross2- Tax2;
//Totals
WorkedTot = Worked1 + Worked2;
GrossTot = Gross1 + Gross2;
TaxTot = Tax1 + Tax2;
NetTot = Net1 + Net2;
//Output.
cout << fixed << setprecision(2);
cout << setw(8) << "EMP"
<< setw(8) << "Hours"
<< setw(8) << "Rate"
<< setw(8) << "Gross"
<< setw(8) << "Tax"
<< setw(8) << "Net"
<< endl;
cout << setw(8) << "1"
<< setw(8) << Worked1
<< setw(8) << PayRate1
<< setw(8) << Gross1
<< setw(8) << Tax1
<< setw(8) << Net1
<< endl;
cout << setw(8) << "2"
<< setw(8) << Worked2
<< setw(8) << PayRate2
<< setw(8) << Gross2
<< setw(8) << Tax2
<< setw(8) << Net2
<< endl;
cout << setw(8) << ""
<< setw(8) << "---"
<< setw(8) << ""
<< setw(8) << "------"
<< setw(8) << "------"
<< setw(8) << "------"
<< endl;
cout << setw(8) << "Totals"
<< setw(8) << WorkedTot
<< setw(8) << ""
<< setw(8) << GrossTot
<< setw(8) << TaxTot
<< setw(8) << NetTot
<< endl << endl;
return 0;
}
My totals for TaxTot and NetTot are both off by a penny. Does anyone have any suggestions??
/* Lab 03P2.cpp
Name: Melissa Beyer
Date: 09/09/01
Purpose: Calculate Payroll
Input: Hours worked and Hourly rate for two employees.
Output: Employee number, Hours worked, hourly rate, gross pay, tax, Net pay. */
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double TaxRate = .2; //Tax rate as decimal 0.2 = 20%.
double Worked1, PayRate1, Gross1, Tax1, Net1;
double Worked2, PayRate2, Gross2, Tax2, Net2;
double WorkedTot, GrossTot, TaxTot, NetTot;
cout << "CPT 267 Lab 03P2 - Melissa Beyer" << endl;
cout << "Program to calculate payroll." << endl << endl;
//Get info for Employee #1.
cout << "Enter hourly rate for Employee #1: ";
cin >> PayRate1;
cout << endl;
cout << "Enter number of hours worked for Employee #1: ";
cin >> Worked1;
cout << endl << endl;
//Get info for Employee #2.
cout << "Enter hourly rate for Employee #2: ";
cin >> PayRate2;
cout << endl;
cout << "Enter number of hours worked for Employee #2: ";
cin >> Worked2;
cout << endl << endl;
//Calculations for Employee #1.
Gross1 = PayRate1 * Worked1;
Tax1 = Gross1 * TaxRate;
Net1 = Gross1 - Tax1;
//Calculations for Employee #2.
Gross2 = PayRate2 * Worked2;
Tax2 = Gross2 * TaxRate;
Net2 = Gross2- Tax2;
//Totals
WorkedTot = Worked1 + Worked2;
GrossTot = Gross1 + Gross2;
TaxTot = Tax1 + Tax2;
NetTot = Net1 + Net2;
//Output.
cout << fixed << setprecision(2);
cout << setw(8) << "EMP"
<< setw(8) << "Hours"
<< setw(8) << "Rate"
<< setw(8) << "Gross"
<< setw(8) << "Tax"
<< setw(8) << "Net"
<< endl;
cout << setw(8) << "1"
<< setw(8) << Worked1
<< setw(8) << PayRate1
<< setw(8) << Gross1
<< setw(8) << Tax1
<< setw(8) << Net1
<< endl;
cout << setw(8) << "2"
<< setw(8) << Worked2
<< setw(8) << PayRate2
<< setw(8) << Gross2
<< setw(8) << Tax2
<< setw(8) << Net2
<< endl;
cout << setw(8) << ""
<< setw(8) << "---"
<< setw(8) << ""
<< setw(8) << "------"
<< setw(8) << "------"
<< setw(8) << "------"
<< endl;
cout << setw(8) << "Totals"
<< setw(8) << WorkedTot
<< setw(8) << ""
<< setw(8) << GrossTot
<< setw(8) << TaxTot
<< setw(8) << NetTot
<< endl << endl;
return 0;
}
My totals for TaxTot and NetTot are both off by a penny. Does anyone have any suggestions??