Finding Netpay Average w/ Employee Class Payroll Program

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 4216 Views - Last Post: 17 August 2012 - 11:58 AM Rate Topic: -----

#1 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Finding Netpay Average w/ Employee Class Payroll Program

Posted 08 August 2012 - 06:49 PM

Ok this problem I have time to work on but I am stuck at the end again. I created an employee class for my payroll program. I have learned a lot more this week and my program executes fine. I want to be able to find the average netpay and display it in my output though. I did this in an original payroll program but now that I'm just learning classes I'd like to learn how to convert it. Here's my program running great:

#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


Now this is what my average netpay looked like in my original payroll program:

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;


Now how would this look if I wanted to convert it to run properly in the new program I created with an employee class? Or what steps would I need to take? Classes are brand new to me and the only thing teaching me all of this is a book. Sometimes it helps to speak to somebody who may be able to explain it better. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Finding Netpay Average w/ Employee Class Payroll Program

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 08 August 2012 - 06:53 PM

You'd want to make employee an array of employees, and then you'd do something like:
totalNetPay += employee[i].calculatenetpay();

Of course, that method shouldn't be void in that case.

This post has been edited by CTphpnwb: 08 August 2012 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 08 August 2012 - 06:57 PM

CTphpnwb now let me ask you.....Would I write this code
totalNetPay += employee[i].calculatenetpay();
right after my:

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


or

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

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 08 August 2012 - 07:08 PM

In post #1 you have two snippets. The second calculates net pay from an array. Using the class you would have an array not of netpay and of type float or double, but of employees and of type payroll.

This post has been edited by CTphpnwb: 08 August 2012 - 07:09 PM

Was This Post Helpful? 0
  • +
  • -

#5 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 08 August 2012 - 07:15 PM

But as you said it's not going to be a void right?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 08 August 2012 - 07:26 PM

View PostLaC0saNostra, on 08 August 2012 - 07:15 PM, said:

But as you said it's not going to be a void right?


Yes. He was talking about line 14 and 41. They should return double and return the netpay that was calculated.
Was This Post Helpful? 0
  • +
  • -

#7 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 08 August 2012 - 07:31 PM

Skydiver haha what's up buddy. Ok well after all of this advice I'm gonna see what I can do here. I'll post back later if I have more questions. Thanks for the information though guys I really appreciate you taking the time to help me out
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 08 August 2012 - 08:03 PM

Ah...Okay, I'm seeing the point of confusion... It's because you have a class named payroll, but you are naming the variable employee. This is what is leading to some of the strange advice you are getting from me and others.

What you want to have happen is something like this:
class Employee
{
public:
    Employee();
    :
    string getName();
    void setName(const string & name);
    :
    double getNetPay();
    :

private:
    string name;
    :
    double _netPay;
    :
};

class Payroll
{
public:
    void Load(string filename);
    :
    void PrintAverageNetPay();
    :

private:
    Employee _employees[5];
};

int main()
{
    Payroll payroll;
    payroll.Load("employees data.txt");
    payroll.PrintAverageNetPay();
    return 0;
}



Edit after: After some more thought this, for our OP's level of skill, I think it wiser to have default constructor and have getters and setters rather than deal with a both the array and a constructor that takes parameters. It's that or make _employees an array of pointers to Employees, and go back to the constructor that takes all the parameters.

This post has been edited by Skydiver: 09 August 2012 - 02:05 AM

Was This Post Helpful? 0
  • +
  • -

#9 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 09 August 2012 - 06:57 PM

Ok this is what I have so far. So your saying I need to make an entirely different class named employee?

#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();
      void findnetpayaverage();
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::printreport(){
     int i=0;
     printheadings();
     while(fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate){
          calculategrosspay();
          calculatetax();
          calculatenetpay();
          printdata();
          i++;   }//WHILE
          }//PRINTREPORT

void payroll::findnetpayaverage(){
     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;

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

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 09 August 2012 - 08:03 PM

No. You want to make the object an element in an array. Then you can loop through the array doing the necessary calculations for each element. Something like this:
#include<iostream>

using namespace std;

class payroll {
public:
  int hoursworked;
  double payrate, taxes, tax_rate;
  
  void set_payroll(double pr) {
    tax_rate = 0.25;
    payrate = pr;
  }
  void set_hours(int hrs) {
    hoursworked = hrs;
  }
  
  void calculate_tax() {
    taxes = hoursworked * payrate * tax_rate;
  }
  
  double get_net() {
    calculate_tax();
    return hoursworked * payrate - taxes;
  }
  
};

int main()
{
  payroll employees[2];
  double totalpay = 0;
  
  employees[0].set_payroll(25.50);
  employees[0].set_hours(39);
  employees[1].set_payroll(42.70);
  employees[1].set_hours(35);
  
  for (int i = 0; i < 2; i++) {
    totalpay += employees[i].get_net();
  }
  cout << "Total net: "<< totalpay << endl << "Average net: "<< totalpay/2<< endl;
  
  
  return 0;
  
}


Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,299
  • Joined: 25-December 09

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 09 August 2012 - 08:33 PM

Also look at how CTphpnwb formatted his code, see how much easier it is to read than your code?

Jim
Was This Post Helpful? 0
  • +
  • -

#12 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 10 August 2012 - 11:31 AM

Alright I'm going to work on it this weekend. I'll post later after I've figured it out or have some more questions. Thank you though for all of the feedback
Was This Post Helpful? 0
  • +
  • -

#13 LaC0saNostra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 22-July 12

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 12 August 2012 - 09:58 AM

#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
class payroll {
public:
  int hoursworked;
  double payrate, taxes, tax_rate;
  void set_payroll(double pr) {
    tax_rate = 0.25;
    payrate = pr;
  }
  void set_hours(int hrs) {
    hoursworked = hrs;
  }
   
  void calculate_tax() {
    taxes = hoursworked * payrate * tax_rate;
  }
  double get_net() {
    calculate_tax();
    return hoursworked * payrate - taxes;
  }
};
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::printreport(){
     int i=0;
     printheadings();
     while(fin>>employeename>>employeeid>>maritalstatus>>hoursworked>>hourlyrate){
          calculategrosspay();
          calculatetax();
          calculatenetpay();
          printdata();
          i++;   }//WHILE
          }//PRINTREPORT

int main()
{
  payroll employees[2];
  double totalpay = 0;
   
  employees[0].set_payroll(25.50);
  employees[0].set_hours(39);
  employees[1].set_payroll(42.70);
  employees[1].set_hours(35);
   
  for (int i = 0; i < 2; i++) {
    totalpay += employees[i].get_net();
    payroll.Load("employee.txt");
  }
  cout << "Total net: "<< totalpay << endl << "Average net: "<< totalpay/2<< endl;
  return 0;
}


Lines 25-27 are where my erros are. Am I on the right track?

Errors:
1.)25 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp expected unqualified-id before "public"
2.)25 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp expected `,' or `;' before "public"
3.)26 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp expected constructor, destructor, or type conversion before ';' token
4.)27 C:\Users\-6\Desktop\Dev-Cpp\ESC\Practice.cpp expected declaration before '}' token
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,299
  • Joined: 25-December 09

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 12 August 2012 - 12:21 PM

Look closely at the following snippet:
class payroll {
public:
.......
};  ///////  This ends the class definition.
public: payroll();


Look at the closing brace of your class, once you end your class you can't define more outside the class definition.

This is one reason you should always use a consistent indentation style. It will make seeing these types of errors easier to see.
class payroll {
   public:
      int hoursworked;
      double payrate, taxes, tax_rate;
      void set_payroll(double pr) {
         tax_rate = 0.25;
         payrate = pr;
      }
      void set_hours(int hrs) {
         hoursworked = hrs;
      }

      void calculate_tax() {
         taxes = hoursworked * payrate * tax_rate;
      }
      double get_net() {
         calculate_tax();
         return hoursworked * payrate - taxes;
      }
};
public:
payroll();
~payroll();
void printreport();
};
payroll::payroll() {
   fin.open("employee2.txt");
}//CONSTRUCTOR
payroll::~payroll() {
   fin.close();
}//DESTRUCTOR



See how much easier it is to see the problem??

Jim
Was This Post Helpful? 1
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,002
  • Joined: 08-August 08

Re: Finding Netpay Average w/ Employee Class Payroll Program

Posted 12 August 2012 - 01:17 PM

Adding to Jim's post, if you need to use comments to show where blocks of code end then you're not indenting properly. This is not something trivial that you can ignore. It's an important part of writing code, so learn it now.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2