473,385 Members | 1,372 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,385 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 8619
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.