Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
This problem involves payroll and uses the selection construct. A possible restatement: An hourly employee's regular payRate is $16.78/hour for hoursWorked <= 40 hours. If hoursWorked > 40 hours, then (hoursWorked -40) is paid at an overtime premium rate of 1.5 * payRate. FICA (social security) tax is 6% and Federal income tax is 14%. Union dues of $10/week are withheld. If there are 3 or more covered dependents, $15 more is withheld for dependent health insurance.
a) Write a program that, on a weekly basis, accepts hours worked then outputs gross pay,
26
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
each withholding amount, and net (take-home) pay.
b) Add 'repeat at user discretion' feature.
I was unpleasantly surprised to find that with early GNU g++ , you cannot use a leading 0 (such as an SSN 034 56 7891) in a sequence of integer inputs. The gnu iostreams library took the integer to be zero and went directly to the next input! You either have to either use an array of char, or 9 char variables to avoid this restriction.
Otherwise, the code is fairly straight forward.
//file Ch2Prob7.cc
27
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
//pay roll problem:
//Inputs: hoursWorked, number of dependents
//Outputs: gross pay, each deduction, net pay //
//This is the 'repeat at user discretion' version //Outline:
//In a real payroll program, each of these values would be //stored in a file after the payroll calculation was printed //to a report. //
//regular payRate = $10.78/hour for hoursWorked <= 40 //hours. //If hoursWorked > 40 hours,
28
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
// overtimePay = (hoursWorked - 40) * 1.5 * PAY_RATE.
//FICA (social security) tax rate is 6%
//Federal income tax rate is 14%. //Union dues = $10/week . //If number of dependents >= 3 // $15 more is withheld for dependent health insurance. //
#include
const double PAY_RATE = 16.78; const double SS_TAX_RATE = 0.06; const double FedIRS_RATE = 0.14; const double STATE_TAX_RATE = 0.05;
29
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch
Problem Solving w/ C++, 6e Instructor’s Resource Guide
Chapter 2
const double UNION_DUES = 10.0; const double OVERTIME_FACTOR = 1.5; const double HEALTH_INSURANCE = 15.0;
int main() {
double hoursWorked, grossPay, overTime, fica,
incomeTax, stateTax, union_dues, netPay; int numberDependents, employeeNumber; char ans;
//set the output to two places, and force .00 for cents
30
Copyright ? 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley