473,883 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
#include<iomani p>
#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::Emplo yee(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>>hoursworke d;
cout<<"Enter the hourly rate"<<endl;
cin>>hourlyrate ;
}

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

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

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

void Employee::displ ay()
{

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 8677
On Tue, 25 Nov 2003 03:37:02 +0000, Randi wrote:

Just a quick peek, there probably is more to say about this:
#include<iostre am>
#include<iomani p>
#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::Emplo yee(string n, float hw, float hr) {
name = n;
hoursworked = hw;
hourlyrate= hr;
}
Better written as:
Employee::Emplo yee(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>>hoursworke d; cout<<"Enter the hourly
rate"<<endl;
cin>>hourlyrate ;
}
This could do with some error checking, but otherwise it's OK for now.
}
float Employee::gross pay()
{
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::overt ime()
{
float ot=0;
float otpay=0;
And here.
if(hoursworked> 40)
{
ot=hoursworked-40;
otpay = ot*1.5;
return otpay;
}
Missing return
}

float Employee::netpa y()
{
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::gross pay()
{
double gp =0;
gp = hoursworked * hourlyrate;
return gp;
}

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

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

void Employee::displ ay()
{

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
1474
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 weekly payroll period reports are produced from the Payroll table by queries that are linked to the Employee details table...salary, base rate, lead rate, wh allowances, status etc...so this is real time data with the current information in the...
1
5329
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 data in this database but whenever I do (without using the Sage payroll client application) I get a permission problem. Now I have the correct username and password, and I tried importing it in a new Access database, but with no luck. Maybe I'm...
0
1160
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 was written in MS VC++ 6.0. I have found some excellent discussions that have helped me define the structures to marshal the data between the unmanaged and managed code. My problem is that my application is not working correctly. The result is...
4
1995
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 was written in MS VC++ 6.0. I have found some excellent discussions that have helped me define the structures to marshal the data between the unmanaged and managed code. My problem is that my application is not working correctly. The result is...
10
7668
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 employee information, and a method within that class to calculate the weekly pay. It seems that the more I read into this, the more confused I get. Any help will be greatly apreciated. Here is my latest code... // Payroll program that...
8
2930
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
11633
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 hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay. Once stop is entered as the employee name, the application should terminate. Her's what I...
1
1626
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 February 2007 Payroll. How will the user close January payroll and then start that of February? I cannot figure out how to write the Program for that. Please kindly help me. Thank you. Akinyemi.
6
5442
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 but when I compile the program I get 13 errors. Can anyone assist me with this? Thank you! Errors I get: C:\Java>javac *.java Payroll3.java:18: illegal start of type while ( !exit ) ^ Payroll3.java:18: <identifier>...
0
9792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10745
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10848
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9575
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7130
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5798
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5992
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3234
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.