user2461929
user2461929

Reputation: 7

Getting totals for columns

I made some changes and am getting the right totals but I've seemed to mess all my alignment up, How could I fix this

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

#include "WeeklyEmp.h" 

using namespace std;

int main()
{
ifstream inFile( "EMPLOYEE.DAT" );

if( !inFile )
{
    cout << "**Error opening file 'EMPLOYEE.DAT'" << endl;
}
else
{
    cout << "   Pay    Hours    Gross    Income    FICA      Net   Employee\n"
         << "  Rate   Worked      Pay      Tax      Tax      Pay   Name\n"
         << " =====   ======   ======   ======   ======   ======   ========\n";

    string fName, lName;
    double hours, rate, totGross = 0.0;
    int exempts;
    char status;
    double totHours = 0.0;
    double totIncome = 0.0;
    double totFica = 0.0; 
    double totNet = 0.0;

    cout << fixed << setprecision(2);

    while( inFile >> fName >> lName >> hours >> rate >> exempts >> status )
    {
        string name = lName + ", " + fName;
        WeeklyEmp anEmp( name, hours, rate, exempts, status ); 

        double net = anEmp.grossPay() - anEmp.incomeTax() - anEmp.FICATax();
        totGross += anEmp.grossPay();
        totIncome += anEmp.incomeTax();
        totFica += anEmp.FICATax();
        totHours += hours;
        totNet += net;

        cout << setfill( ' ' ) << setw(6) << rate << setfill( ' ' )
             << setw(9) << hours
             << setw(9) << anEmp.grossPay()
             << setw(9) << anEmp.incomeTax()
             << setw(9) << anEmp.FICATax()
             << setw(9) << net 
             << "   " 
             << anEmp.name()
             << endl;
        cout << "         ------   ------   ------   ------   ------"
             << setw(6) << "Totals"
             << setw(9) << totHours
             << setw(9) << totGross
             << setw(9) << totIncome
             << setw(9) << totHours
             << setw(9) << totNet; 

    }


}

cin.get();
return 0;
}

This is how the output looks right now with out proper alignment, plus I'm getting triple's of "-----" and "Totals"

  Pay    Hours    Gross    Income    FICA      Net   Employee
 Rate   Worked      Pay      Tax      Tax      Pay   Name
=====   ======   ======   ======   ======   ======   ========
10.45    38.00   397.10    26.16    30.38   340.56   Greene, Ross
     ------   ------   ------   ------   ------Totals    38.00   397.10    2
6.16    38.00   340.56 12.00    42.00   516.00    89.42    39.47   387.11   Kris
tner, Mary
     ------   ------   ------   ------   ------Totals    80.00   913.10   11
5.58    80.00   727.66  9.99    30.50   304.69    36.34    23.31   245.04   Nich
olson, Melissa
     ------   ------   ------   ------   ------Totals   110.50  1217.79   15
1.92   110.50   972.71 11.57    40.00   462.80    63.49    35.40   363.91   Wood
ley, Samuel
     ------   ------   ------   ------   ------Totals   150.50  1680.59   21
5.41   150.50  1336.62

This is the way it should be, with totals on there.

  Pay    Hours    Gross    Income    FICA      Net   Employee
 Rate   Worked      Pay      Tax      Tax      Pay   Name
=====   ======   ======   ======   ======   ======   ========
10.45    38.00   397.10    26.16    30.38   340.56   Greene, Ross
12.00    42.00   516.00    89.42    39.47   387.11   Kristner, Mary
 9.99    30.50   304.69    36.34    23.31   245.04   Nicholson, Melissa
11.57    40.00   462.80    63.49    35.40   363.91   Woodley, Samuel
        ------   ------   ------   ------   ------
Totals

Upvotes: 0

Views: 109

Answers (1)

Boris
Boris

Reputation: 622

You can calculate the totals in the loop:

double sumHours = 0;

while( inFile >> fName >> lName >> hours >> rate >> exempts >> status )
{
    // ...

    sumHours += hours;

    // ...    
}

cout << sumHours;

Upvotes: 1

Related Questions