#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
#define FEDERAL_WITHHOLDING 55.77
#define STATE_EXEMPTION 38.46
#define SOCIAL_SECURITY .0765
// Setting up all prototypes for the functions
double CalculateGrossPay( double, int );
double CalculateFederalTax( double, int );
double CalculateStateTax( double, int );
double CalculateSocialSecurity( double );
int ValidInt( string, int , int );
double ValidDouble( string, double, double );
void BarGraph( int );
int WeekDisplay ( int );
int main()
{
// Declaring all variables
char yesOrNo;
int hoursWorked,numExemptions,numWeeks=0, numWeekDisplay=1;
double wage,grossPay,federalTax,stateTax,sstate,netPay,totalGross=0,totalNet=0;
//Setting the precision
cout<<setprecision(2)<<fixed<<showpoint;
// Asking the user whether they want to calculate their weekly pay
cout << "Do you want to calculate your weekly pay? (Y/N) ";
cin >> yesOrNo;
// Setting up a while statement for when the user enters 'y' or 'Y'
while( yesOrNo == 'y' || yesOrNo == 'Y' )
{
// Setting up calling statements
hoursWorked = ValidInt("\nEnter the number of hours worked - (minimum: 0, maximum: 60) ",0,60);
wage = ValidDouble("Enter the hourly wage - (minimum: $0.00, maximum: $30.00) ",0.,30.);
numExemptions = ValidInt("Enter the number of exemptions - (minimum: 0, maximum: 5) ",0,5);
// Setting variables equal to the functions to be executed
grossPay = CalculateGrossPay( wage, hoursWorked );
federalTax = CalculateFederalTax( grossPay, numExemptions );
stateTax = CalculateStateTax( grossPay, numExemptions );
sstate = CalculateSocialSecurity( grossPay );
netPay = grossPay - ( sstate + federalTax + stateTax );
numWeekDisplay = WeekDisplay ( numWeekDisplay);
// Displaying the results
cout << "\nWeek " << numWeekDisplay << ":" << endl;
cout << "You worked " << hoursWorked << " hours at $" << wage << " per hour.\n";
cout << "Gross Pay: " << grossPay << endl;
cout << "Federal Tax: " << federalTax << endl;
cout << "State Tax: " << stateTax << endl;
cout << "Social Security: " << sstate << endl;
cout << "Net Pay: " << netPay << endl;
totalGross+=grossPay; // total gross pay = gross pay + total gross pay
totalNet+=netPay; // total net pay = net pay + total net pay
numWeeks++; // increase # of weeks every time user wants to calculate weekly pay
numWeekDisplay++;
// Ask the user whether they want to calculate weekly pay again
cout << "\nDo you want to calculate your weekly pay? (Y/N) ";
cin >> yesOrNo;
}
// If the user enters no, display this at the end
cout << "\nTotal Gross Pay: " << totalGross << endl;
cout << "Total Net Pay: " << totalNet << endl;
BarGraph( numWeeks );
cout << endl;
system ("pause");
return 0;
}
// Setting up all functions
/*****************************************************************************
double CalculateGrossPay( )
The purpose of this function is to calculate and return an employee's gross
pay for one week. It takes two arguments: a double representing the employee's
hourly wage and an integer representing the number of hours that the employee
worked for one week.
*****************************************************************************/
double CalculateGrossPay( double wage, int hoursWorked )
{
//Declaring variables
double grossPay;
// Setting up if-else statement
if( hoursWorked > 40)
{
// formula to be used if hours worked is over 40
grossPay = ( wage * 40) + ( wage * 1.5 * (hoursWorked - 40) );
}
else
{
// formulat to be used if hours worked is less than or equal to 40
grossPay=(wage* hoursWorked);
}
return grossPay;
}
/*****************************************************************************
double CalculateFederalTax( )
The purpose of this function is to calculate and return the federal income tax
to withhold from an employee. It takes two arguments: a double that represents
the employee's gross pay and an integer argument that represents the number of
exemptions claimed by the employee.
*****************************************************************************/
double CalculateFederalTax( double grossPay, int numExemptions )
{
// Declaring variables
double federal;
// Formula to be used to determine how much federal tax is taken out of pay
grossPay = grossPay - (FEDERAL_WITHHOLDING * numExemptions );
// Setting up if-else-if statement to be used
if( grossPay < 51 )
{
federal = 0;
}
else if( grossPay < 552 )
{
federal = 0.15 * grossPay;
}
else if( grossPay < 1196 )
{
federal = 75.15+( grossPay - 552 ) * 0.28;
}
else if( grossPay < 2662 )
{
federal = 255.47 + ( grossPay -1196 ) * 0.31;
}
else if(grossPay <5750)
{
federal = 709.93 + ( grossPay - 2662 ) * 0.36;
}
else
{
federal = 1821 + ( grossPay - 5750 ) * 0.396;
}
return federal;
}
/*****************************************************************************
double CalculateStateTax( )
The purpose of this function is to calculate and return the state income tax
to withhold from an employee. It takes two arguments: a double that represents
the employee's gross pay and an integer that represents the number of exemptions
claimed by the employee.
*****************************************************************************/
double CalculateStateTax( double grossPay, int numExemptions )
{
// Declaring variables
double state;
// Setting up if else statements
// if gross pay is less than or equal to state exemption * number of exemptions
if( grossPay <= STATE_EXEMPTION * numExemptions)
{
// state tax is 0
state = 0;
}
else
{
// state tax is calculated by the following formula
state = 0.03 * ( grossPay - (STATE_EXEMPTION * numExemptions));
}
return state;
}
/*****************************************************************************
double CalculateSocialSecurity( )
The purpose of this function is to calculate and return the amount of social
security to withhold from an employee. It takes one argument: a double that
represents the employee's gross pay.
*****************************************************************************/
double CalculateSocialSecurity( double grossPay )
{
// Declaring variables
double SS;
// Formula used to calculate social security tax
SS = SOCIAL_SECURITY * grossPay;
return SS;
}
/*****************************************************************************
int ValidInt( )
The purpose of this function is to get an integer from the user that is within
a certain range. It takes three arguments: a string that is used as a prompt
to the user to tell them what number they are entering, an integer that
represents the smallest integer that the user can enter, and an integer
that represents the largest integer that the user can enter. The second and
third arguments will be used to ensure that the user enters a valid value. If
an invalid value is entered, display an error message and prompt the user to
re-enter a value. This should be done repeatedly until the user enters a valid
integer. The function returns the valid integer.
*****************************************************************************/
int ValidInt( string prompt, int lowerBound, int upperBound )
{
// Declaring variables
double num;
cout << prompt << ": ";
cin >> num;
// while loop to be used if data entered is above or below the boundaries
while( num < lowerBound || num > upperBound)
{
// Display this "invalid statement" if data is out of the boundaries
cout << "Error: Entry out of range. Try again.\n";
cout << prompt << ": ";
cin >> num; // store the value entered into num
}
return num;
}
/*****************************************************************************
double ValidDouble( )
This function is exactly like ValidInt, except that it takes two doubles,
rather than integers.
*****************************************************************************/
double ValidDouble( string prompt, double lowerBound, double upperBound )
{
// Declaring variables
int num;
cout << prompt << ": ";
cin >> num;
// while loop to be used if data entered is above or below the boundaries
while ( num < lowerBound || num > upperBound )
{
// Display this "invalid statement" if data is out of the boundaries
cout << "Error: Entry out of range. Try again. \n";
cout << prompt << ": ";
cin >> num; // store the value entered into num
}
return num;
}
/*****************************************************************************
void BarGraph( )
The purpose of this function is to display a line of asterisks equal to the
number of weeks that weekly pay has been calculated. It takes one argument: an
integer that represents the number of weekly pay calculations that have been
performed. This function returns nothing.
*****************************************************************************/
void BarGraph( int numWeeks )
{
// Display this cout statement
cout << "\nTotal Number of Weeks: ";
// Used to create the bar graph for the number of weeks of pay that were calculated
for ( int i=0; i < numWeeks; i++ )
{
cout << "* ";
}
}
/*****************************************************************************
int WeekDisplay ()
The purpose of this function is to display the number of the weekly salary
that is being calculated and to have it increment by one each time the user
says yes.
*****************************************************************************/
int WeekDisplay (int numWeekDisplay)
{
for (int i=1; i < numWeekDisplay; i++ )
{
cout << numWeekDisplay;
}
return numWeekDisplay;
}
output
Do you want to calculate your weekly pay? (Y/N) y
Enter the number of hours worked - (minimum: 0, maximum: 60) : 45
Enter the hourly wage - (minimum: $0.00, maximum: $30.00) : 30
Enter the number of exemptions - (minimum: 0, maximum: 5) : 5
Week 1:
You worked 45 hours at $30.00 per hour.
Gross Pay: 1425.00
Federal Tax: 241.51
State Tax: 36.98
Social Security: 109.01
Net Pay: 1037.49
Do you want to calculate your weekly pay? (Y/N)