d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Payroll C++ Hw Issue > Why Wont My Tax Rate Work?
Add Reply New Topic New Poll
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Apr 10 2014 07:13pm
Someone please tell me why I am getting the garbage I am with my tax rate, I am so frustrated and this is due at 10PM

Heres the input and output
Code
Test Case #1:
------------------------------------------------------------------------
Input Sample
5
1000 .035
2000 .055
3000 .072
4000 .085
5000 .1
17
1001 43 51.0
1002 34 10.5
1003 56 32.0
1004 41 80.5
1005 43 40.0
1006 0 12.0
1007 44 88.5
1008 40 55.5
1009 -8 60.0
1010 70 88.5
1011 49 60.0
1012 18 20.0
1013 55 88.5
1014 21 5.0
1015 65 34.5
1016 35 08.5
1017 40 20.5

Output Sample
Enter the number of levels for the tax table: Enter taxable wages and tax rates:
Enter the number of employees to process: Enter employee ID, hours and hourly rate:

Employee Payroll with 17 entries.

EmpID Gross Pay Tax Rate Net Pay
------ --------- --------- ---------
1001 2269.50 7.20% 2106.10
1002 357.00 3.50% 344.51
1003 2048.00 7.20% 1900.54
1004 3340.75 8.50% 3056.79
1005 1780.00 5.50% 1682.10
1006 Zero or negative hours! Unable to process!!
1007 4071.00 10.00% 3663.90
1008 2220.00 7.20% 2060.16
1009 Zero or negative hours! Unable to process!!
1010 Abnormal overtime hours! Unable to process!!
1011 3210.00 8.50% 2937.15
1012 360.00 3.50% 347.40
1013 5531.25 12.00% 4867.50
1014 105.00 3.50% 101.32
1015 Abnormal overtime hours! Unable to process!!
1016 297.50 3.50% 287.09
1017 820.00 3.50% 791.30
------ --------- --------- ---------
Total gross pay: $ 26410.00


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

const int MAX_TAXTABLE = 5;
const int MAX_EMPLOYEES = 25;
const int MAX_HOURS = 60;
const int NORMAL_HOURS = 40;
const float OVERTIME_RATE = 1.5;
const float RATE_TO_PCT = 100.0;



void GetTaxTable ( float taxable[], float taxrate[], int & numTax );
void GetEmpHours (int empid[], int hours[], float hourlyrate[],
int & numEmp);
void PrintHeader (int numEmp);
void ProcessPayroll (const int empid[], const int hours[],
const float hourlyrate[], int numEmp,
const float taxable[], const float taxrate[],
int numTax);
bool OverHoursLimit (int hours);
int FindTaxLevel (float wages, const float taxable[], int size);
float ComputeWages (int hours, float hourlyrate);
void PrintEmpWages (int empid, float wages, float rate, float netpay);

int main()
{
int empid[MAX_EMPLOYEES], hours[MAX_EMPLOYEES], numTax, numEmp;
float taxable[MAX_TAXTABLE], taxrate[MAX_TAXTABLE],
hourlyrate[MAX_EMPLOYEES];

GetTaxTable(taxable, taxrate, numTax);
GetEmpHours(empid, hours, hourlyrate, numEmp);
PrintHeader(numEmp);
ProcessPayroll(empid, hours, hourlyrate, numEmp, taxable, taxrate, numTax);
return 0;
}

//---------------------------------------------------------------------
// This function reads the tax table and stores it in a pair of
// parallel arrays. The function should read the size of the table
// first, and then read the wage levels and tax rates.
// Params: in, in, inout
//---------------------------------------------------------------------
void GetTaxTable(float taxable[], float taxrate[], int & numTax)
{
cin >> numTax;
for ( int i = 0; i < numTax; i++ )
{
cin >> taxable[i] >> taxrate[i];
}
}
//---------------------------------------------------------------------
// This function reads the log table of employees and stores the table
// in three parallel arrays. The function should read the size of the
// table first, and then read the employee IDs, work hours, and hourly
// rates.
// Params: in, in, in, inout
//---------------------------------------------------------------------
void GetEmpHours(int empid[], int hours[], float hourlyrate[],
int & numEmp)
{
cin >> numEmp;
for ( int i = 0; i < numEmp; i++ )
{
cin >> empid[i] >> hours[i] >> hourlyrate[i];
}
}
//---------------------------------------------------------------------
// This function prints out the header of the resulting payroll table
// given the number of entries in the table.
// Params: in
//---------------------------------------------------------------------
void PrintHeader(int numEmp)
{
cout << "Employee Payroll with ";
if ( numEmp == 1 )
cout << numEmp << " entry" << endl;
else
cout << numEmp << " entries" << endl;
cout << " EmpID Gross Pay Tax Rate Net Pay" << endl
<< "------ --------- --------- ---------" << endl;
}
//---------------------------------------------------------------------
// This function processes the data stored in the parallel arrays,
// computes the wages and tax withheld, and prints out the results.
// Params: in, in, in, in, in, in, in
//---------------------------------------------------------------------
void ProcessPayroll(const int empid[], const int hours[],
const float hourlyrate[], int numEmp,
const float taxable[], const float taxrate[],
int numTax)
{
int empID = 0, hour = 0, taxL = 0;
float hourRate = 0, taxableLevel = 0;
float taxRate = 0, netPay = 0, gross = 0;
for ( int i=0; i < numEmp; i++ )
{
if ( hours[i] <= 0 )

cout << setw(6) << empid[i] << " "
<< "Zero or negative hours! Unable to process!!" << endl;

else if ( OverHoursLimit(hours[i]) )

cout << setw(6) << empid[i] << " "
<< "Abnormal overtime hours! Unable to process!!" << endl;


else
{
hour = hours[i];
hourRate = hourlyrate[i];
taxableLevel = taxable[i];
gross = ComputeWages ( hour, hourRate );
taxL = FindTaxLevel ( gross, taxable , numTax);
taxRate = taxrate[taxL];
netPay = gross - (taxRate * gross);
PrintEmpWages ( empid[i], gross, taxRate, netPay);
}
}
}

//---------------------------------------------------------------------
// This function determines whether a given number of work hours
// exceeds the limit of overtime hours.
// Params: in
//---------------------------------------------------------------------
bool OverHoursLimit(int hours)
{
if ( hours > MAX_HOURS )
return true;
else
return false;
}

//---------------------------------------------------------------------
// Given the wages, this function finds and returns the index of the
// associated tax rate in the tax table. It returns a -1 if the wages
// exceed the maximum tax level.
// Params: in, in, in
//---------------------------------------------------------------------
int FindTaxLevel(float wages, const float taxable[], int size)
{
for ( int i = 0; i < size; i++ )
{
if ( taxable [i] <= wages )
return i;
}
return -1;
}
//---------------------------------------------------------------------
// This function computes and returns the wages based on the hours
// worked and the hourly rate.
// Params: in, in
//---------------------------------------------------------------------
float ComputeWages(int hours, float hourlyrate)
{
float grossPay = 0.0;
float overtimePay = 0.0;
if (hours > NORMAL_HOURS)
{
grossPay = NORMAL_HOURS * hourlyrate +
( ( hours - NORMAL_HOURS ) * hourlyrate * OVERTIME_RATE );
}
else if ( hours <= NORMAL_HOURS )
grossPay = hours * hourlyrate;
return grossPay;
}
//---------------------------------------------------------------------
// This function prints out employee's ID, wages, tax rate, and the
// net pay.
// Params: in, in, in, in
//---------------------------------------------------------------------
void PrintEmpWages(int empid, float wages, float rate, float netpay)
{
cout << setw(6) << empid << setw(10) << setprecision(2) << fixed
<< showpoint << wages << setw(10) << ( RATE_TO_PCT * rate )
<< "%" << setw(10) << netpay << endl;
}
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Apr 10 2014 07:55pm
Code
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;\

const int MAX_TAXTABLE = 5;
const int MAX_EMPLOYEES = 25;
const int MAX_HOURS = 60;
const int NORMAL_HOURS = 40;
const float OVERTIME_RATE = 1.5;
const float RATE_TO_PCT = 100.0;



void GetTaxTable ( float taxable[], float taxrate[], int & numTax );
void GetEmpHours (int empid[], int hours[], float hourlyrate[],
int & numEmp);
void PrintHeader (int numEmp);
void ProcessPayroll (const int empid[], const int hours[],
const float hourlyrate[], int numEmp,
const float taxable[], const float taxrate[],
int numTax);
bool OverHoursLimit (int hours);
int FindTaxLevel (float wages, const float taxable[], int size);
float ComputeWages (int hours, float hourlyrate);
void PrintEmpWages (int empid, float wages, float rate, float netpay);

int main()
{
int empid[MAX_EMPLOYEES], hours[MAX_EMPLOYEES], numTax, numEmp;
float taxable[MAX_TAXTABLE], taxrate[MAX_TAXTABLE],
hourlyrate[MAX_EMPLOYEES];

GetTaxTable(taxable, taxrate, numTax);
GetEmpHours(empid, hours, hourlyrate, numEmp);
PrintHeader(numEmp);
ProcessPayroll(empid, hours, hourlyrate, numEmp, taxable, taxrate, numTax);
return 0;
}

//---------------------------------------------------------------------
// This function reads the tax table and stores it in a pair of
// parallel arrays. The function should read the size of the table
// first, and then read the wage levels and tax rates.
// Params: in, in, inout
//---------------------------------------------------------------------
void GetTaxTable(float taxable[], float taxrate[], int & numTax)
{
cin >> numTax;
for ( int i = 0; i < numTax; i++ )
{
cin >> taxable[i] >> taxrate[i];
}
}
//---------------------------------------------------------------------
// This function reads the log table of employees and stores the table
// in three parallel arrays. The function should read the size of the
// table first, and then read the employee IDs, work hours, and hourly
// rates.
// Params: in, in, in, inout
//---------------------------------------------------------------------
void GetEmpHours(int empid[], int hours[], float hourlyrate[],
int & numEmp)
{
cin >> numEmp;
for ( int i = 0; i < numEmp; i++ )
{
cin >> empid[i] >> hours[i] >> hourlyrate[i];
}
}
//---------------------------------------------------------------------
// This function prints out the header of the resulting payroll table
// given the number of entries in the table.
// Params: in
//---------------------------------------------------------------------
void PrintHeader(int numEmp)
{
cout << "Employee Payroll with ";
if ( numEmp == 1 )
cout << numEmp << " entry" << endl;
else
cout << numEmp << " entries" << endl;
cout << " EmpID Gross Pay Tax Rate Net Pay" << endl
<< "------ --------- --------- ---------" << endl;
}
//---------------------------------------------------------------------
// This function processes the data stored in the parallel arrays,
// computes the wages and tax withheld, and prints out the results.
// Params: in, in, in, in, in, in, in
//---------------------------------------------------------------------
void ProcessPayroll(const int empid[], const int hours[],
const float hourlyrate[], int numEmp,
const float taxable[], const float taxrate[],
int numTax)
{
int empID = 0, hour = 0, taxL = 0;
float hourRate = 0, taxableLevel = 0, totalGross = 0;
float taxRate = 0, netPay = 0, gross = 0;
for ( int i=0; i < numEmp; i++ )
{
if ( hours[i] <= 0 )

cout << setw(6) << empid[i] << " "
<< "Zero or negative hours! Unable to process!!" << endl;

else if ( OverHoursLimit(hours[i]) )

cout << setw(6) << empid[i] << " "
<< "Abnormal overtime hours! Unable to process!!" << endl;


else
{
hour = hours[i];
hourRate = hourlyrate[i];
taxableLevel = taxable[i];
gross = ComputeWages ( hour, hourRate );
taxL = FindTaxLevel ( gross, taxable , numTax);
taxRate = taxrate[taxL];
netPay = gross - (taxRate * gross);
PrintEmpWages ( empid[i], gross, taxRate, netPay);
}
totalGross += gross;
}
cout << "------ --------- --------- ---------" << endl;
cout << setprecision(2) << fixed << showpoint
<< "Total gross pay: $ " << totalGross <<endl;
}

//---------------------------------------------------------------------
// This function determines whether a given number of work hours
// exceeds the limit of overtime hours.
// Params: in
//---------------------------------------------------------------------
bool OverHoursLimit(int hours)
{
if ( hours > MAX_HOURS )
return true;
else
return false;
}

//---------------------------------------------------------------------
// Given the wages, this function finds and returns the index of the
// associated tax rate in the tax table. It returns a -1 if the wages
// exceed the maximum tax level.
// Params: in, in, in
//---------------------------------------------------------------------
int FindTaxLevel(float wages, const float taxable[], int size)
{
for ( int i = 0; i < size; i++ )
{
if ( taxable [i] <= wages )
return i;
}
return -1;
}
//---------------------------------------------------------------------
// This function computes and returns the wages based on the hours
// worked and the hourly rate.
// Params: in, in
//---------------------------------------------------------------------
float ComputeWages(int hours, float hourlyrate)
{
float grossPay = 0.0;
float overtimePay = 0.0;
if (hours > NORMAL_HOURS)
{
grossPay = NORMAL_HOURS * hourlyrate +
( ( hours - NORMAL_HOURS ) * hourlyrate * OVERTIME_RATE );
}
else if ( hours <= NORMAL_HOURS )
grossPay = hours * hourlyrate;
return grossPay;
}
//---------------------------------------------------------------------
// This function prints out employee's ID, wages, tax rate, and the
// net pay.
// Params: in, in, in, in
//---------------------------------------------------------------------
void PrintEmpWages(int empid, float wages, float rate, float netpay)
{
cout << setw(6) << empid << setw(10) << setprecision(2) << fixed
<< showpoint << wages << setw(10) << ( RATE_TO_PCT * rate )
<< "%" << setw(10) << netpay << endl;
}

heres where im at now still issues with tax rate and now total gross is off by a few thousand

This post was edited by bensfriend1 on Apr 10 2014 07:59pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 11 2014 12:11am
Looks like there are 2 issues.

First, your FindTaxLevel function has a bug. It should be

Code
if ( taxable [i] >= wages )


Second, there seems to be a missed requirement. Based on the test output, if the employee's wages exceed 5000 then the tax rate should be 12%. So I added this:

Code
const float MAX_TAX_RATE = 0.12;


and then when calculating the taxRate I did this:

Code
taxRate = taxL == -1 ? MAX_TAX_RATE : taxrate[taxL];


Doing so yields the correct results in the sample output.


Sorry I missed your PM. I know you missed your deadline, but I hope this still helps.

This post was edited by Minkomonster on Apr 11 2014 12:12am
Member
Posts: 2,252
Joined: Jan 7 2008
Gold: 0.00
Apr 11 2014 08:19am
Quote (Minkomonster @ Apr 11 2014 12:11am)
Looks like there are 2 issues.

First, your FindTaxLevel function has a bug. It should be

Code
if ( taxable [i] >= wages )


Second, there seems to be a missed requirement. Based on the test output, if the employee's wages exceed 5000 then the tax rate should be 12%. So I added this:

Code
const float MAX_TAX_RATE = 0.12;


and then when calculating the taxRate I did this:

Code
taxRate = taxL == -1 ? MAX_TAX_RATE : taxrate[taxL];


Doing so yields the correct results in the sample output.


Sorry I missed your PM. I know you missed your deadline, but I hope this still helps.


yes it does, I ended up adding some things similar to these which made it work and I finished before the deadline so no worse for wear.

I appreciate the reply.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll