// TESTING
//------------------------------------------------------------------------------
// TEST CASE 1
//
// DESCRIPTION
// Performs an acceptance test of the entire program.
//
// INPUT DATA (Note that the input data is read from a filenamed payroll.txt)
// Simpson Homer
// 15.25 84
// 2
//
// EXPECTED OUTPUT
// -----------------------------
// EMPLOYEE: Simpson, Homer
//
// PAY RATE: $ 15.25
// HOURS: 84.00
// GROSS PAY: $ 1311.50
// MED INS DEDUCT: $ 110.13
// 401K DEDUCT: $ 78.69
// FED TAX GROSS PAY: $ 1122.68
// TAX - FEDERAL: $ 89.48
// TAX - OASDI: $ 69.61
// TAX - MEDICARE: $ 16.28
// TAX - STATE: $ 38.62
// TAX - TOTAL: $ 213.98
// NET PAY: $ 908.70
// -----------------------------
//
// ACTUAL OUTPUT
// RESULT: Write FAIL/pass
#include // For exit()
#include // For ifstream, ofstream
#include // For setprecision(), setw()
#include // For endl, fixed
#include // For string class
using namespace std;
//==============================================================================
// FUNCTION PROTOTYPES
//
// Students: Some of the functions may require prototypes. Forthose that do,
// write the prototype in this section.
//==============================================================================
//==============================================================================
// NAMED CONSTANTS
//
// Students: Define named constants in this section.
//==============================================================================
// Define an int named constant ERR_OPEN_INPUT_FILE which isequivalent to 1.
int ERR_OPEN_INPUT_FILE = 1;
// Define an int named constant ERR_OPEN_OUTPUT_FILE which isequivalent to 2.
int ERR_OPEN_INPUT_FULE = 2;
// This is the percentage rate for calculating the OASDIdeduction (this is com-
// monly known as social security). It is 6.2% of the employee'sfederal taxable
// gross pay.
const double SOCIAL_SECURITY = 0.062;
// All employees are required to contribute 6.0% of their pretaxgross pay to
// the company 401K plan.
const double FOUR01K_PLAN = 0.06;
// Define a double constant named MEDICARE_RATE initialized to0.0145.
// This is the percentage rate for calculating the medicarededuction. It is
// 1.45% of the employee's federal taxable gross pay.
// ???
const double MEDICARE_RATE = 0.0145;
// These constants are the monthly costs for each of the medicalinsurance
// plans. The amount an employee pays depends on his or hermedical insurance
// status (see the group of constants following this group).
const double MEDICAL_INSURANCE_DEDUCT_EMPLOYEE_ONLY = 32.16; //Employee Only
const double MEDICAL_INSURANCE_DEDUCT_EMPLOYEE_PLUS_ONE = 64.97;// Employee + One
const double MEDICAL_INSURANCE_DEDUCT_FAMILY = 110.13; //Family
// These constants match the numbers for the employee's medicalinsurance status
// that will be in the input file.
const int MEDICAL_INSURANCE_STATUS_EMPLOYEE_ONLY = 0; //Employee Only
const int MEDICAL_INSURANCE_STATUS_EMPLOYEE_PLUS_ONE = 1; //Employee + One
const int MEDICAL_INSURANCE_STATUS_FAMILY = 2; // Family
void ErrorExit (string msg)
{
cout << msg << endl;
exit (-1);
}
//------------------------------------------------------------------------------
// FUNCTION: calc_gross_pay()
//
// Calculates and returns an employee's gross pay which is basedon the number
// of hours worked (in parameter hrs_worked) and the employee'spay rate (in
// parameter pay_rate).
//------------------------------------------------------------------------------
double calc_gross_pay(double pay_rate, double hrs_worked)
{
double gross_pay;
if (hrs_worked <= 80){
gross_pay = hrs_worked * pay_rate;
} else {
gross_pay = (80 * pay_rate) + (hrs_worked - 80) * (1.5 *pay_rate);
}
return gross_pay;
}
//------------------------------------------------------------------------------
// FUNCTION: calc_med_ins_deduct()
//
// Determines and returns the employee's medical insurancededuction which is
// based on the employee's medical insurance status in parametermed_ins_status.
//------------------------------------------------------------------------------
double calc_med_ins_deduct (int med_ins_status){
double MedInsDeduct = 0;
if (med_ins_status ==MEDICAL_INSURANCE_STATUS_EMPLOYEE_ONLY){
MedInsDeduct = MEDICAL_INSURANCE_DEDUCT_EMPLOYEE_ONLY;}
else if (med_ins_status ==MEDICAL_INSURANCE_STATUS_EMPLOYEE_PLUS_ONE){
MedInsDeduct = MEDICAL_INSURANCE_DEDUCT_EMPLOYEE_PLUS_ONE;}
else {MedInsDeduct = MEDICAL_INSURANCE_DEDUCT_FAMILY;}
return MedInsDeduct;
}
//------------------------------------------------------------------------------
// FUNCTION: calc_tax_fed()
//
// Calculates and returns the employee's federal income taxwhich is based on
// his or her federal taxable gross pay (in parameterfed_tax_gross_pay) and the
// federal tax withholding percentage table in the lab projectdocument.
//------------------------------------------------------------------------------
double calc_tax_fed(double fed_tax_gross_pay){
double tax_fed = 0;
if(fed_tax_gross_pay >= 384.62 && fed_tax_gross_pay< 1413.67){
tax_fed = fed_tax_gross_pay * 0.0797;
} else if (fed_tax_gross_pay >= 1413.67 &&fed_tax_gross_pay < 2695.43){
tax_fed = 0.1275 * fed_tax_gross_pay;
} else if (fed_tax_gross_pay >= 2695.43){
tax_fed = 0.195 * fed_tax_gross_pay;
}
return tax_fed;}
//------------------------------------------------------------------------------
// FUNCTION: calc_tax_state()
//
// Calculates and returns the employee's state income tax whichis based on his
// or her federal taxable gross pay (in parameterfed_tax_gross_pay) and the
// state tax withholding percentage table in the lab projectdocument.
//------------------------------------------------------------------------------
double calc_tax_state(double fed_tax_gross_pay)
{
double tax_state = 0;
if (fed_tax_gross_pay < 961.54) {
tax_state = fed_tax_gross_pay * 0.0119;
} else if (fed_tax_gross_pay < 2145.66) {
tax_state = fed_tax_gross_pay * 0.0344;
} else {
tax_state = fed_tax_gross_pay * 0.0774;
}
return tax_state;
}
//------------------------------------------------------------------------------
// open_input_file(ifstream&, string) -> nothing
//
// See the comments in the function header of this function inmain.cpp of Lab
// Project 6 for more information on how this function operates.Copy-and-paste
// the code for this function from your Lab 6 main.cpp sourcecode file.
//------------------------------------------------------------------------------
void open_input_file(ifstream& fin , string);
void terminate (string msg){
cout << \"Could not open Payroll.txt\";
}
//------------------------------------------------------------------------------
// open_output_file(ofstream&, string) -> nothing
//
// See the comments in the function header of this function inmain.cpp of Lab
// Project 6 for more information on how this function operates.Copy-and-paste
// the code for this function from your Lab 6 main.cpp sourcecode file.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// terminate(string, int) -> nothing
//
// See the comments in the function header of this function inmain.cpp of Lab
// Project 6 for more information on how this function operates.Copy-and-paste
// the code for this function from your Lab 6 main.cpp sourcecode file.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// FUNCTION: main()
//
// This is the starting point of execution.
//------------------------------------------------------------------------------
int main()
{
// Define an ifstream object named 'fin' to read from the inputfile. Then
// call open_input_file() passing \"payroll.txt\" as the name ofthe file to
// be opened. If the file could not be opened for reading, thenopen_input
// _file() will not return.
ifstream fin;
if(!fin){
ErrorExit (\"Payroll.txt could not be opened for writing\");}
// Read the employee's last and first names from the inputfile.
string first_name, last_name;
fin >> last_name >> first_name;
// Read the employee's pay rate from the input file.
double pay_rate;
fin >> pay_rate;
// Read the employee's hours worked from the input file.
double hrs_worked;
fin >> hrs_worked;
// Read the employee's medical insurance status from the inputfile.
int med_ins_status;
fin >> med_ins_status;
// Close the input file because we're done reading from it.
fin.close();
// Call calc_gross_pay() to calculate the employee's gross pay.The input
// params are pay_rate and hrs_worked. The return value isassigned to vari-
// able gross_pay which is defined as a double.
double gross_pay = calc_gross_pay(pay_rate, hrs_worked);
// Calculate the employee's mandatory contribution to his/her401k. This is
// gross_pay multiplied by the 401K rate.
double four01k_deduct = gross_pay * FOUR01K_PLAN;
// Call calc_med_ins_deduct() to determine the employee'smedical insurance
// deduction which is based on the employee's medical insurancestatus
// stored in med_ins_status. The return value is assigned tovariable
// med_ins_deduct which is defined as a double.
double med_ins_deduct = calc_med_ins_deduct(med_ins_status);
// Calculate the employee's federal taxable gross pay which isgross_pay
// minus deductions for medical insurance and 401K.
double fed_tax_gross_pay = gross_pay - med_ins_deduct -four01k_deduct;
// Call calc_tax_fed() to calculate the employee's federalincome tax. The
// input parameter is fed_tax_gross_pay and the returned valueis assigned
// to variable tax_fed which is defined as a double.
double tax_fed = calc_tax_fed(fed_tax_gross_pay);
// Calculate the amount withheld for OASDI and store intax_oasdi.
double tax_oasdi = fed_tax_gross_pay * SOCIAL_SECURITY;
// Calculate the amount withheld for medicare and store intax_medicare.
double tax_medicare = fed_tax_gross_pay * MEDICARE_RATE;
// Call calc_tax_state() to determine the employee's state taxdeduction.
// The input parameter is fed_tax_gross_pay and the returnedvalue is
// assigned to variable tax_state which is defined as adouble.
double tax_state = calc_tax_state(fed_tax_gross_pay);
// Calculate the employee's total tax which is the sum ofhis/her federal
// tax, OASDI tax, medicare tax, and state tax. Assign totax_total.
double tax_total = tax_fed + tax_oasdi + tax_medicare +tax_state;
// Calculate the employee's net pay which is federal taxablegross pay with
// taxes deducted. Assign to net_pay.
double net_pay = fed_tax_gross_pay - tax_total;
// Define an ofstream object named fout to write to the outputfile. Then
// call open_output_file() passing fout and \"paycheck.txt\" asthe name of
// the file to be opened. If the file could not be opened forwriting, then
// open_output_file() will not return.
ofstream fout;
fout.open(\"Paycheck.txt\");
if(!fout){
ErrorExit(\"Payroll.txt couldn't be opened for writing.\");
}
// Configure fout so real numbers will be printed in fixednotation with two
// digits after the decimal point.
fout << fixed << setprecision(2);
// Configure fout so the numbers will be printed right-justifiedin their
// respective fields.
fout << right;
// Output the employee paycheck. All numerical values are outputin a field
// of width 8.
fout << \"-----------------------------\" << endl;
fout << \"EMPLOYEE: \" << last_name << \", \"<< first_name << endl << endl;
fout << \"PAY RATE: $\" << setw(8) << pay_rate<< endl;
fout << \"HOURS: \" << setw(8) << hrs_worked<< endl;
fout << \"GROSS PAY: $\" << setw(8) << gross_pay<< endl;
fout << \"MED INS DEDUCT: $\" << setw(8) <fout << \"401K DEDUCT: $\" << setw(8) <fout << \"FED TAX GROSS PAY: $\" << setw(8) <fout << \"TAX - FEDERAL: $\" << setw(8) <fout << \"TAX - OASDI: $\" << setw(8) <fout << \"TAX - MEDICARE: $\" << setw(8) <fout << \"TAX - STATE: $\" << setw(8) <fout << \"TAX - TOTAL: $\" << setw(8) <fout << \"NET PAY: $\" << setw(8) << net_pay<< endl;
fout << \"-----------------------------\" << endl;
// Close the output file.
fout.close();
// pause the system for a while
system(\"pause\");
// Return 0 from main() to indicate to the OS that the programterminated
// normally.
return 0;
}