473,327 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Payroll Program using class structure

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::overtime()
{
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();
}

Jul 22 '05 #1
1 8607
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::overtime()
{
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::overtime()
{
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

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: zexx | last post by:
Need some advise.... Payroll program Access97. Payroll is printed then posted to a hx table thro' a query with the payroll calculations that freeze that payroll's numbers for that period. The...
1
by: cheungs7 | last post by:
Hi all, Wondering whether you can anyone help me. We have Sage Payroll (and accounting/payroll software), which has a MS Access database where the data is stored. Now I want to access the...
0
by: FishingScout | last post by:
I am re-writing an MS VC++ 6.0 application in Visual Studio 2005 VB.NET. In order for my new application to communicate with some hardware (an RFID reader) I need to communicate with a DLL that...
4
by: FishingScout | last post by:
I am re-writing an MS VC++ 6.0 application in Visual Studio 2005 VB.NET. In order for my new application to communicate with some hardware (an RFID reader) I need to communicate with a DLL that...
10
by: ycg0771 | last post by:
I'm trying to modify the following program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the...
8
by: John Sitka | last post by:
Hi, There are four pay types standard shiftpremium doubletime doubletimepremium each hour a person books can be one of these types
5
by: jbailey006 | last post by:
I am trying to add the following information to a payroll program I am working on for assignment. Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the...
1
by: Akinyemi | last post by:
I have almost finished writing my Payroll Program. But I am wondering how the program can be used for different months. For example, after, say January 2007 Payroll, the user would want to prepare...
6
by: aureao4 | last post by:
I'm new to Java and programming. I'm trying to code a payroll program and continue getting errors. The program worked last week, this week I have to add set, get and a class. I've written the class...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.