How to add up the totals in a column?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lafayettejohnson
    New Member
    • Jul 2010
    • 7

    #1

    How to add up the totals in a column?

    I need to figure out how to add up the totals for all the columns except pate rate and employee name. I think Iam on the right track with line 35 but now Iam stuck.
    Code:
    #include <fstream> 
    #include <iostream> 
    #include "weekemp.h" 
    #include "compfun.h" 
    #include "string"     
    using namespace std;
    int main()
    {
      weeklyEmp anEmp;
      string fName, lName, name;
      double hours, rate, totGross;
      int exempts;
      string status;
      
      ifstream inFile("employee.dat");
    
      
      if(! inFile)
      {
        cout << "**Error opening file 'employee.dat'" << endl;
      }
      else
      {
       //double net = emp.grossPay() - emp.incomeTax() - emp.FICATax(); 
        decimals(cout, 2);
        cout << "   Pay  Hours  Gross  Income   FICA     Net  Employee" << endl;
        cout << "  Rate Worked    Pay     Tax    Tax     Pay  Name" << endl;
        cout << " ===== ====== ======  ======  =====  ======  ==============" << endl;
        
        while(inFile >> fName >> lName >> hours >> rate >> exempts >> status)
        {
          name = lName + ", " + fName;
          anEmp = weeklyEmp(name, hours, rate, exempts, status);
          double net = anEmp.grossPay() - anEmp.incomeTax() - anEmp.FICATax(); 
           totGross += anEmp.grossPay();
          cout.width(6);
          cout << rate; 
          cout.fill(' ');
    
          cout.width(7);
          cout << hours;
    
          cout.width(8);
          cout << anEmp.grossPay();
    
          cout.width(8);
          cout << anEmp.incomeTax();
    
          cout.width(7);
          cout << anEmp.FICATax();
    
          cout.width(8);
          cout << net;
    
          cout << "  ";
          cout << anEmp.name();
    
          cout << endl;
        }
    	
      }
    
      return 0;
    }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    I find your question difficult to understand given that you are reading the lines of data from a file. Why don`t you just include the extra line of totals in the same file and read them? Clearly I`m missing something!

    Comment

    • Joseph Martell
      Recognized Expert New Member
      • Jan 2010
      • 198

      #3
      I think that I understand what you are going for lafayette. This is the same structure we use for a lot of our reporting programs at the company I work for.

      I think we need a little bit more information though. What are you stuck on, exactly?

      You are looping through your file and adding up the gross pay with totGross. You will need to do something similar for all of the columns you wish to sum. This seems to match your stated goal pretty well.

      Comment

      Working...