Payroll Program W/ Employee Class

Aug 8, 2012 at 2:42am
Man I'm learning more and more every day and some of it is tough and some not too bad. I am now using an employee class for my payroll program and I have it executing fine. My issue is I want to find the average netpay and display it in my program. Here's my program first:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
class payroll{
      ifstream fin;
      char employeeid[12];
      char employeename[20];
      char maritalstatus;
      int hoursworked,overtime;
      double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay;
      void calculategrosspay();
      void calculatetax();
      void calculatenetpay();
      void printheadings();
      void printdata();
public: payroll();
        ~payroll();
        void printreport();  };
payroll::payroll(){
         fin.open("employee.txt");   }//CONSTRUCTOR
         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;      }//IF
else grosspay=hoursworked*hourlyrate;   }//CALCULATEGROSSPAY

void payroll::calculatetax(){
if(grosspay>=500) taxrate=.30;
        else if(grosspay>200.00) taxrate=.20;
        else taxrate=.10;
        if(maritalstatus=='S'||maritalstatus=='s')
                taxrate=taxrate+.05;
        taxamount=grosspay*taxrate;   }//CALCULATETAXRATE

void payroll::calculatenetpay(){
        netpay=grosspay-taxamount;   }//CALCULATENETPAY

void payroll::printheadings(){
     cout<<setw(45)<<"-PAYROLL REPORT-"<<endl;
     cout<<"--------------------------------------------------------------------"<<endl;
     cout<<"NAME        ID      HW OT  RT-PAY   OT-PAY GROSS"
             "    TAX    NETPAY"<<endl;
     cout<<"--------------------------------------------------------------------"<<endl;
}//PRINTHEADINGS

void payroll::printdata(){
       cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
       cout<<setw(6)<<employeename<<setw(12)<<employeeid<<setw(4)
       <<hoursworked<<setw(3)<<overtime<<setw(8)<<regularpay<<setw(8)
       <<overtimepay<<setw(8)<<grosspay<<setw(8)<<taxamount<<setw(8)
       <<netpay<<endl;   }//PRINTDATA

void payroll::printreport(){
     int i=0;
     printheadings();
     while(fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate){
          calculategrosspay();
          calculatetax();
          calculatenetpay();
          printdata();
          i++;   }//WHILE
          }//PRINTREPORT

main(){
     payroll employee;
     employee.printreport();
     system("PAUSE");
        }//MAIN 

This is my calculations in my original payroll program before converting:
1
2
3
4
5
6
7
for(i; i<n; i++){
       totalNetPay +=netpay[i];
        cout<<""<<id[i]<<"\t"<<hoursworked[i]<<"\t"<<hourlyrate[i]
        <<"\t"<<overtimepay[i]<<"\t\t"<<grosspay[i]<<"\t\t"
        <<taxamount[i]<<"\t"<<netpay[i]<<endl;
        }//FOR
        cout<<"The net pay average of the employees are: "<<totalNetPay/i<<endl;


Where would I place this in my new program?At the end of my void payroll::printdata(){ or void payroll::printreport(){

Are there any different rules too since it's in a class?
Last edited on Aug 9, 2012 at 1:51am
Aug 8, 2012 at 3:14am
Well class members, just like regular variables and functions, should have a certain "naming" scheme to them. I'd believe printreport would be a better option since it should print the report of all the information "summarized" in the class. But the names are so close together, it's a little hard to understand the difference.

Ultimately, it's up to you.
Aug 8, 2012 at 3:20am
What....?

I see no class of any kind in that code.

I also don't see void payroll::printdata() or void payroll::printreport()

From what i can see, I don't see anything obviously wrong, other then your program being poorly structured.
Aug 10, 2012 at 1:31am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
class payroll{
      ifstream fin;
      char employeeid[12];
      char employeename[20];
      char maritalstatus;
      int hoursworked,overtime;
      double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay;
      void calculategrosspay();
      void calculatetax();
      void calculatenetpay();
      void printheadings();
      void printdata();
public: payroll();
        ~payroll();
        void printreport();  };
payroll::payroll(){
         fin.open("employee2.txt");   }//CONSTRUCTOR
         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;      }//IF
else grosspay=hoursworked*hourlyrate;   }//CALCULATEGROSSPAY

void payroll::calculatetax(){
if(grosspay>=500) taxrate=.30;
        else if(grosspay>200.00) taxrate=.20;
        else taxrate=.10;
        if(maritalstatus=='S'||maritalstatus=='s')
                taxrate=taxrate+.05;
        taxamount=grosspay*taxrate;   }//CALCULATETAXRATE

void payroll::calculatenetpay(){
        netpay=grosspay-taxamount;   }//CALCULATENETPAY

void payroll::printheadings(){
     cout<<setw(45)<<"-PAYROLL REPORT-"<<endl;
     cout<<"--------------------------------------------------------------------"<<endl;
     cout<<"NAME        ID      HW OT  RT-PAY   OT-PAY GROSS"
             "    TAX    NETPAY"<<endl;
     cout<<"--------------------------------------------------------------------"<<endl;
}//PRINTHEADINGS

void payroll::printdata(){
       cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
       cout<<setw(6)<<employeename<<setw(12)<<employeeid<<setw(4)
       <<hoursworked<<setw(3)<<overtime<<setw(8)<<regularpay<<setw(8)
       <<overtimepay<<setw(8)<<grosspay<<setw(8)<<taxamount<<setw(8)
       <<netpay<<endl;   }//PRINTDATA
       
void payroll::findsum(){

void payroll::printreport(){
     int i=0;
     printheadings();
     while(fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate){
          calculategrosspay();
          calculatetax();
          calculatenetpay();
          printdata();
          findsum();
          i++;   }//WHILE
          findavg();
          }//PRINTREPORT

main(){
     payroll employee;
     employee.printreport();
     system("PAUSE");
        }//MAIN 


I need to figure out my findsum and find avg functions for the netpay. Any tips where to begin? No arrays this time around?
Last edited on Aug 13, 2012 at 12:48am
Aug 22, 2012 at 2:22am
I figured it out! Here is my final product:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
class payroll{
              ifstream fin;
              char employeeid[12];
              char employeename[20];
              char maritalstatus;
              int hoursworked,overtime;
              double hourlyrate,overtimepay,regularpay,grosspay,taxrate,taxamount,netpay, sum, average;
              void calculategrosspay();
              void calculatetax();
              void calculatenetpay();
              void printheadings();
              void printdata();
              void findsum(int);
              double findavg(double, int);
 
public: payroll();
        ~payroll();
        void printreport();  };
        payroll::payroll(){
        fin.open("employee2.txt");   }//CONSTRUCTOR
        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;      }//IF
 
      else {grosspay=hoursworked*hourlyrate;   
      regularpay = hoursworked * hourlyrate;
      overtime =0;
      overtimepay = 0;}
}//CALCULATEGROSSPAY
 
 
void payroll::calculatetax(){
     if(grosspay>=500) taxrate=.30;
     else if(grosspay>200.00) taxrate=.20;
     else taxrate=.10;
     if(maritalstatus=='S'||maritalstatus=='s')
     taxrate=taxrate+.05;
     taxamount=grosspay*taxrate;   
}//CALCULATETAXRATE
 
void payroll::calculatenetpay(){
     netpay=grosspay-taxamount;   }//CALCULATENETPAY
 
 void payroll::printheadings(){
      cout<<setw(45)<<"-PAYROLL REPORT-"<<endl;
      cout<<"--------------------------------------------------------------------"<<endl;
      cout<<"NAME        ID      HW OT  RT-PAY   OT-PAY GROSS"
      "    TAX    NETPAY"<<endl;
      cout<<"--------------------------------------------------------------------"<<endl;
}//PRINTHEADINGS
 
void payroll::printdata(){
     cout<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint);
     cout<<setw(6)<<employeename<<setw(12)<<employeeid<<setw(4)
     <<hoursworked<<setw(3)<<overtime<<setw(8)<<regularpay<<setw(8)
     <<overtimepay<<setw(8)<<grosspay<<setw(8)<<taxamount<<setw(8)
     <<netpay<<endl;   }//PRINTDATA
 
void payroll::findsum(int i){
                          sum+=netpay;
}
double payroll::findavg(double sum, int i){
       average = sum/i;
       cout<<endl<<"The netpay average is "<< average<<endl;
       }
void payroll::printreport(){
     int i=0;
     printheadings();
     while(fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate){
          calculategrosspay();
          calculatetax();
          calculatenetpay();
          printdata();
          i++; 
          findsum(i);
          }//WHILE
          findavg(sum, i);
          }//PRINTREPORT
 
main(){
payroll employee;
employee.printreport();
system("PAUSE");
}//MAIN 
Topic archived. No new replies allowed.