获取列的总计

时间:2013-06-14 07:31:52

标签: c++ add

我做了一些修改并得到了正确的总数,但我似乎弄乱了所有的调整,我怎么能解决这个问题

#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;
}

这就是输出看起来如何正确对齐,加上我得到的三倍“-----”和“总计”

  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

这是应该的方式,总数就在那里。

  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

1 个答案:

答案 0 :(得分:1)

您可以计算循环中的总数:

double sumHours = 0;

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

    sumHours += hours;

    // ...    
}

cout << sumHours;