All was good and properly aligned until I changed the variable type from int to double for GrossPay, GrP and totGrP. After the Hours column (from Gross Pay on), in all the rows where there are fewer digits, the Gross Pay, Tax, and Net Pay columns are left aligned, totally out of sync with the rest of the table (the columns are all right aligned otherwise, and should be)... What can I do about this?
Code (output to follow):
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
double GrossPay(int,int);
double Tax(double);
int main ()
{
ifstream empdata;
empdata.open("employees.txt");
//if(!empdata.is_open()) {
// cout << "File was not opened correctly" << endl;
// cout << "program halted";
// return 0;
//}
int totEmpl, ID, hrsWrk;
double hrlPay;
double GrP = 0;
double Tx = 0;
double Net;
int noTx, bigHrs, bigTxP;
double totGrP, totTx, avgTx, totNet;
totGrP=0; noTx=0;
bigHrs=0; bigTxP=0;
totTx=0; totNet=0;
cout.setf(ios::fixed,ios::floatfield);
cout << setw(2) << "#" << "\t"
<< setw(5) << "ID" << "\t"
<< setw(7) << "$/Hr" << "\t"
<< setw(4) << "Hrs" << "\t"
<< setw(6) << "Gross" << "\t"
<< setw(9) << "Tax" << "\t"
<< setw(9) << "Net" << endl << endl;
empdata >> totEmpl;
for (int count=1; count<=totEmpl; count++) {
empdata >> ID >> hrlPay >> hrsWrk;
GrP = GrossPay(hrlPay,hrsWrk);
Tx = Tax(GrP);
Net = GrP-Tx;
cout << setw(2) << count << "\t"
<< setw(5) << ID << "\t"
<< setw(7) << setprecision(2) << hrlPay << "\t"
<< setw(4) << hrsWrk << "\t"
<< setw(6) << GrP << "\t"
<< setw(9) << setprecision(2) << Tx << "\t"
<< setw(9) << setprecision(2) << Net << endl << endl;
totGrP=totGrP+GrP;
totTx=totTx+Tx;
totNet=totNet+Net;
if (Tx==0)
noTx++;
if (Tx>=100) {
bigHrs=bigHrs+hrsWrk;
bigTxP++;
}
}
avgTx=totTx/totEmpl;
cout << "\n";
cout << "Total gross pay: " << "$" << totGrP << endl;
cout << "Total tax: " << "$" << totTx << endl;
cout << "Total net pay: " << "$" << totNet << endl;
cout << "Average tax: " << "$" << avgTx << endl;
cout << "Number of employees who paid no tax: " << noTx << endl;
cout << "Average number of hours worked for employees who paid at least $100 in taxes: " << bigHrs/bigTxP << endl;
empdata.close();
return 0;
}
double GrossPay (int r, int w) {
int excess, pay;
if (w<=40)
pay=r*w;
else if (w>40) {
excess = w-40;
pay = r*40+(excess*r*2);
}
return pay;
}
double Tax (double g) {
double t;
if (g>500)
t = (((g-500)*10)/100)+150;
else if (g<=500 && g>=200)
t = ((g-200)*5)/100;
else t = 0;
return t;
}
Output:
# ID $/Hr Hrs Gross Tax Net
1 283 20.00 5 100.00 0.00 100.00
2 156 50.00 20 1000.00 200.00 800.00
3 385 15.00 60 1200.00 220.00 980.00
4 739 100.00 20 2000.00 300.00 1700.00
5 555 50.00 50 3000.00 400.00 2600.00
6 412 100.00 1 100.00 0.00 100.00
7 904 150.00 8 1200.00 220.00 980.00
8 716 75.00 3 225.00 1.25 223.75
9 872 35.00 70 3500.00 450.00 3050.00
10 202 20.00 50 1200.00 220.00 980.00
11 194 200.00 10 2000.00 300.00 1700.00
12 810 40.00 4 160.00 0.00 160.00
Total gross pay: $15685.00
Total tax: $2311.25
Total net pay: $13373.75
Average tax: $192.60
Number of employees who paid no tax: 3
Average number of hours worked for employees who paid at least $100 in taxes: 36
Just put it in code brackets so maybe the text would be in fixed alignment... I don't know... Either way, I explained above what's wrong with the alignment, if it doesn't show here.