Payroll Program using class structure

Discussion in 'C++' started by Randi, Nov 25, 2003.

  1. Randi

    Randi Guest

    Hi,
    Looking for some help with this payrool project I have for class. This is
    what the instructor asks for so far. I have it working without errors but
    am getting some funky numbers. I am not sure if they would be correct
    anyways. Below is the instructions.
    and below that is what I have. Any tips or hints would be highly
    appreciated.

    Regards,
    Kelsey

    You have been hired to write a program that will perform payroll tasks. The
    program must be written using object-orientated programming concepts. This
    implies that you MUST use the C++ class structure in this program. This
    payroll program will contain an Employee class to store the employee's name,
    hourly rate, hours worked, gross pay, and net pay. The class should have
    operations that will perform the following tasks.



    1. An operation to initialize the hourly rate to a minimum wage of
    $5.50 per hour and the hours worked to 0 when the employee is defined.

    2. An operation to obtain the employee's name, hourly rate, and hours
    worked from the user.

    3. An operation to return the weekly gross pay, including overtime pay,
    where overtime is paid at the rate of time-and-a-half for any hours worked
    over 40.

    4. An operation to return the weekly net pay, based on a 30% deduction
    for taxes and benefits.

    5. An operation to display the employee's name, gross pay, and net pay.



    Your program should demonstrate that it works by allowing the user to run
    the program and enter in an employee's name, pay rate, and hours. Then
    output the name, net pay and gross pay to the screen.



    My Crappy code..



    #include<iostream>
    #include<iomanip>
    #include <string>
    using namespace std;

    class Employee
    {
    private:
    string name;
    float hourlyrate;
    float hoursworked;
    float gp;
    float np;
    float otpay;
    public:
    Employee();
    Employee(string empName, float hoursW, float rate);

    void getinfo();
    float grosspay();
    float overtime();
    float netpay();
    void display();

    };

    Employee:: Employee()
    {
    name = " ";
    hourlyrate =0;
    hoursworked =0;
    }

    Employee::Employee(string n, float hw, float hr)
    {
    name = n;
    hoursworked = hw;
    hourlyrate= hr;
    }

    void Employee:: getinfo()
    {
    cout<<"Enter the emplyees name"<<endl;
    cin>>name;
    cout<<"Enter the hours worked"<<endl;
    cin>>hoursworked;
    cout<<"Enter the hourly rate"<<endl;
    cin>>hourlyrate;
    }

    float Employee::grosspay()
    {
    float gp =0;
    gp = hoursworked * hourlyrate;
    return gp;
    }

    float Employee::eek:vertime()
    {
    float ot=0;
    float otpay=0;
    if(hoursworked>40)
    {
    ot=hoursworked-40;
    otpay = ot*1.5;
    return otpay;
    }
    }

    float Employee::netpay()
    {
    float np =0;
    np = (otpay + gp)*.70;
    return np;
    }

    void Employee::display()
    {

    cout<<name<<" "<<gp<<" "<<np<<endl;

    }

    void main()
    {
    float gp =0;
    float otpay =0;
    float np =0;

    Employee book(" ",0,5.5);
    book.getinfo();
    gp = book.grosspay();
    otpay = book.overtime();
    np = book.netpay();
    book.display();
    }
     
    Randi, Nov 25, 2003
    #1
    1. Advertising

  2. On Tue, 25 Nov 2003 03:37:02 +0000, Randi wrote:

    Just a quick peek, there probably is more to say about this:

    > #include<iostream>
    > #include<iomanip>
    > #include <string>
    > using namespace std;
    >
    > class Employee
    > {
    > private:
    > string name;
    > float hourlyrate;
    > float hoursworked;
    > float gp;
    > float np;
    > float otpay;


    You don't want float. It's precission is terrible. You either want double,
    long (count cents, not dollars), or some long long (non standard
    extention) if your compiler supports that last one.

    > public:
    > Employee();
    > Employee(string empName, float hoursW, float rate);
    >
    > void getinfo();
    > float grosspay();
    > float overtime();
    > float netpay();
    > void display();
    >
    > };
    >
    > Employee:: Employee()
    > {
    > name = " ";
    > hourlyrate =0;
    > hoursworked =0;
    > }


    This does not do what the assignment asked, initialise hoursworked to
    $5.50.

    >
    > Employee::Employee(string n, float hw, float hr) {
    > name = n;
    > hoursworked = hw;
    > hourlyrate= hr;
    > }


    Better written as:
    Employee::Employee(string n, float hw, float hr)
    : name(n), hoursworked(hw), hourlyrate(hr)
    {
    }

    (same goes for default constructor above).

    It does not matter for this small program, but it will in the future.
    Better get into the habit of using initialiser lists.

    >
    > void Employee:: getinfo()
    > {
    > cout<<"Enter the emplyees name"<<endl; cin>>name; cout<<"Enter the
    > hours worked"<<endl; cin>>hoursworked; cout<<"Enter the hourly
    > rate"<<endl;
    > cin>>hourlyrate;
    > }


    This could do with some error checking, but otherwise it's OK for now.

    > }
    > float Employee::grosspay()
    > {
    > float gp =0;


    This local variable masks the member with the same name. I think your main
    error is this.

    > gp = hoursworked * hourlyrate;
    > return gp;
    > }
    >
    > float Employee::eek:vertime()
    > {
    > float ot=0;
    > float otpay=0;


    And here.

    > if(hoursworked>40)
    > {
    > ot=hoursworked-40;
    > otpay = ot*1.5;
    > return otpay;
    > }


    Missing return

    > }
    >
    > float Employee::netpay()
    > {
    > float np =0;


    And here.

    > np = (otpay + gp)*.70;
    > return np;
    > }
    >


    Rethink your logic. You are calculating lot's of things, but only when
    the user calls some members. That is probably not correct. Don't use
    member variables to store what can be calculated, only calculate and only
    when needed.

    Something along these lines.

    class Employee
    {
    private:
    string name;
    double hourlyrate;
    double hoursworked;
    public:
    Employee();
    Employee(string empName, double hoursW, double rate);

    void getinfo();
    double grosspay();
    double overtime();
    double netpay();
    void display();

    };

    [ snip ]

    // This stays the same, except float->double
    double Employee::grosspay()
    {
    double gp =0;
    gp = hoursworked * hourlyrate;
    return gp;
    }

    // note return statement has moved
    double Employee::eek:vertime()
    {
    double ot=0;
    double otpay=0;
    if(hoursworked>40)
    {
    ot=hoursworked-40;
    otpay = ot*1.5;
    }
    return otpay;
    }

    float Employee::netpay()
    {
    return (overtime() + gp)*.70;
    }

    void Employee::display()
    {

    cout<<name<<" "<< grosspay() <<" "<< netpay() <<endl;

    }

    // main returns an int. Really.
    //void main()
    int main()
    {
    Employee book(" ",0,5.5);
    book.getinfo();
    book.display();
    }

    Oops, I think I just made your homework for you. :) Important point is
    you understand why this is better, so play with it, study it and create
    your own SAP-system.

    HTH,
    M4
     
    Martijn Lievaart, Nov 25, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,842
    Thomas Weidenfeller
    Oct 12, 2005
  2. jd
    Replies:
    3
    Views:
    364
  3. kinglv
    Replies:
    0
    Views:
    369
    kinglv
    Aug 18, 2009
  4. Brandize Nelson
    Replies:
    0
    Views:
    402
    Brandize Nelson
    Oct 13, 2010
  5. Brandize Nelson
    Replies:
    0
    Views:
    390
    Brandize Nelson
    Oct 14, 2010
Loading...

Share This Page