Payroll Program using class structure

R

Randi

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();
}
 
M

Martijn Lievaart

#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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top