trying to add a sum function in a class program - help

This is a discussion on trying to add a sum function in a class program - help within the C++ Programming forums, part of the General Programming Boards category; I am just starting to program and have an assignment where we need to use classes for an employee payroll ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    1

    Question trying to add a sum function in a class program - help

    I am just starting to program and have an assignment where we need to use classes for an employee payroll program. Everything is working fine until I try to add a function that calculates the sum of netpays. I think that I need to enter a loop in the findsumnetpay funtion but have not had any luck. Please help to guide me in the right direction.
    Here is the code:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <iomanip>
     
     using namespace std;
    //file input output stream
     
      
    //function prototypes
    class payroll{
    public:  ifstream fin;
             char employeeid[6];
          char employeename[12];
          int hoursworked, overtime;
          double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay,sumnetpay;
          void calculategrosspay();
          void calculatetax();
          void calculatenetpay();
          double findsumnetpay();
          void printheadings();
          void printdata();
          
    public:payroll();
    ~payroll();
    void printreport(); };
    payroll::payroll(){
                       fin.open("employee8.in");}
    payroll::~payroll(){
                       fin.close(); }//DESTRUCTOR
                       
    void payroll::calculategrosspay(){
         if(hoursworked>40){
         overtime = hoursworked-40;
         regularpay = hoursworked * hourlyrate;
         overtimepay = overtime *(hourlyrate *1.5);
         grosspay = regularpay + overtimepay; } 
    else grosspay = hoursworked * hourlyrate;  }//CALCULATGROSSPAY
         
    void payroll::calculatetax(){
         taxamount = grosspay * .30;  }//CALCULATETAX
    
    void payroll::calculatenetpay(){
         netpay = grosspay - taxamount;
         }//CALCULATENETPAY
         
    double payroll::findsumnetpay(){
         sumnetpay = sumnetpay+netpay;
         return sumnetpay;}//FINDSUMNETPAY    
     
    void payroll::printheadings(){
         cout<<setw(45)<<"-PAYROLL REPORT-"<<endl;
         cout<<"-------------------------------------------------------------------------"<<endl;
         cout<<"NAME      ID        HW     RT-PAY     OT-PAY     GROSS"
         "    TAX      NETPAY"<<endl;
         cout<<"-------------------------------------------------------------------------"<<endl;
         }//PRINTHEADINGS
         
    void payroll::printdata(){
         cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
         cout<<setw(8)<<employeename<<setw(6)<<employeeid<<setw(8)<<hoursworked<<
         setw(11)<<regularpay<<setw(11)<<overtimepay
         <<setw(10)<<grosspay<<setw(10)<<taxamount<<setw(10)
         <<netpay<<endl;}
    
    void payroll::printreport(){
         int i;
         printheadings();
         while(fin>>employeename>>employeeid>>hoursworked>>hourlyrate){
            calculategrosspay();
            calculatetax();
            calculatenetpay();
            findsumnetpay();
            printdata();
    
            i++;  }//WHILE        
         
         //FINDSUMNETPAY
             
            }//PRINTREPORT
            
    int main(){
         payroll employee;
         employee.printreport();
         cout<<employee.sumnetpay<<endl;
         system("Pause");
         }//MAIN

  2. #2
    Banned
    Join Date
    Jan 2009
    Posts
    30
    Dear friend,

    Your code looks excellent thus far, have you yet studied iostreams? Perhaps this would be a good time to use cin.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So sumnetpay starts out as "random". You then add something to it, which makes it "random". You then print it out, which means you get "random". Maybe you should start sumnetpay to have some initial value.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    I am going to give you readability advice.
    First, don't put the "}" on the ending row; put it on the NEXT row. It helps readability considerably.
    Secondly, you MUST indent better. Refer to http://cpwiki.sourceforge.net/Indentation if you don't know how.
    Thirdly, try putting spaces here and there in your code. For example:
    Code:
    void payroll::printdata(){
         cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
         cout<<setw(8)<<employeename<<setw(6)<<employeeid<<setw(8)<<hoursworked<<
         setw(11)<<regularpay<<setw(11)<<overtimepay
         <<setw(10)<<grosspay<<setw(10)<<taxamount<<setw(10)
         <<netpay<<endl;}
    Readable code:
    Code:
    void payroll::printdata()
    {
         cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);
         cout << setw(8) << employeename << setw(6) << employeeid << setw(8) << hoursworked
              << setw(11) << regularpay << setw(11) << overtimepay
              << setw(10) << grosspay << setw(10) << taxamount << setw(10)
              << netpay << endl;
    }
    And lastly, try indenting some more if you break one line into several so you can easily identify that those additional lines belong to the line above.
    See how much easier it became to read the code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-16-2005, 11:55 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21