// Title: A Simple Payroll Program -- Using Functions // // Programmer: Bill McQuain // ID Number: 000-00-0000 // Compiler: Microsoft Visual C++ ver 4.0 // Platform: Pentium 75, Windows 95 // Date: October 27, 1996 // // Purpose: // // This program reads information about an arbitrary number of // employees from an input file named "inpay.dat": // // - ID number // - age // - gross monthly salary // - insurance plan type // // and computes the correct deductions for: // // - federal income tax // - social security tax // - insurance fee // - net monthly salary. // // It then prints out a labeled table of results, showing for // each employee the ID number, gross salary, insurance fee, // income and social security tax deductions and net salary. // In addition, the averages of each category are computed and // printed. // #include #include // Function prototypes. It's not necessary to include names for // the parameters here, but I recommend it because it helps keep // things straight. float FedTax(float Gross); float Insurance(char Plan, int Age); void PrintHeader(ofstream& outPay); void PrintLine(ofstream& outPay, int IdNum, float Gross, float Fee, float FIT, float SSI, float Net); void PrintAvg(ofstream& outPay, float TotalGross, float TotalIns, float TotalFIT, float TotalSSI, float TotalNet, int NumEmployees); // Beginning of main function: int main() { // Named constants --- the use of named constants is required for // this program. Other named are used in the functions that find // the income tax and insurance amounts. // const float SSRate = 0.0675f; // SSI tax rate // Variables: ifstream inPay; // input file stream ofstream outPay; // output file stream int IdNum, // employee ID number Age, // employee age NumEmployees; // number of employees float GrossPay, // employee gross pay NetPay, // employee net pay SSI, // employee SS tax FIT, // employee income tax InsFee, // employee insurance fee TotalGross, // total of all gross pay TotalNet, // total of all net pay TotalIns, // total of all insurance fees TotalSSI, // total of all SS tax TotalFIT; // total of all income tax char InsPlan; // employee insurance plan // Initializations: NumEmployees = 0; // Initialize counters and running totals TotalGross = 0.0f; // to zero. TotalNet = 0.0f; TotalIns = 0.0f; TotalSSI = 0.0f; TotalFIT = 0.0f; inPay.open("inpay.dat"); // open input and output files outPay.open("outpay.dat"); // Set up for floating point output: outPay.setf(ios::fixed, ios::floatfield); outPay.setf(ios::showpoint); // Print header info to output file: PrintHeader(outPay); // Read first line of data from input file: inPay >> IdNum >> Age >> GrossPay >> InsPlan; // Process and read next line until there's no more data: while (inPay) { NumEmployees++; // Count employees. TotalGross += GrossPay; // Update total gross pay. inPay.ignore(200, '\n'); // Skip to next line. InsFee = Insurance(InsPlan, Age); // Calculate insurance fee TotalIns += InsFee; // Update total insurance // fees. FIT = FedTax(GrossPay); // Calculate income tax amt. TotalFIT += FIT; // Update total income // taxes. SSI = GrossPay * SSRate; // Calculate SS tax. TotalSSI += SSI; // Update total SS taxes. NetPay = GrossPay - InsFee - FIT - SSI; // Calculate net pay. TotalNet += NetPay; // Update total net pay. // Write results to output file: PrintLine(outPay, IdNum, GrossPay, InsFee, FIT, SSI, NetPay); // Read next line of input: inPay >> IdNum >> Age >> GrossPay >> InsPlan; } // End of while loop. // Print out averages (if appropriate): PrintAvg(outPay, TotalGross, TotalIns, TotalFIT, TotalSSI, TotalNet, NumEmployees); // Close files. inPay.close(); outPay.close(); return NumEmployees; } ///////////////////////////////////////////////////////////////////// // Function to calculate the proper insurance fee for an employee. // Parameters: // InsPlan character 'B' or 'D', indicating plan selected // Age employee's age // // The function doesn't need to modify the value of either of these // parameters, so they are passed in by value (no ampersand). // // Return value: // InsFee insurance fee charged to employee // float Insurance(char Plan, int Age) { const float HiPlanB = 225.00f; // constants for insurance fees const float MidPlanB = 150.00f; const float LoPlanB = 100.00f; const float HiPlanD = 300.00f; const float MidPlanD = 200.00f; const float LoPlanD = 140.00f; const char PlanB = 'B'; // constants for plan types const char PlanD = 'D'; float Fee= 0.0f; // insurance fee charged switch (Plan) { // Calculate insurance fee case 'B':{if (Age <= 35) // for Plan B Fee = LoPlanB; else if (Age <= 65) Fee = MidPlanB; else Fee = HiPlanB; break; } case 'D':{if (Age <= 35) // for Plan D Fee = LoPlanD; else if (Age <= 65) Fee = MidPlanD; else Fee = HiPlanD; break; } } return Fee; } ///////////////////////////////////////////////////////////////////// // Function to calculate the proper income tax for an employee. // Parameter: // GrossPay employee's gross pay amount // // The function doesn't need to modify the value of this parameter, // so it is passed in by value (no ampersand). // // Return value: // FIT income tax withheld for employee // float FedTax(float Gross) { const float HiTax = 0.33f; // constants for tax rates const float MidTax = 0.28f; const float LoTax = 0.15f; const float HiIncome = 3000.00f; // constants for income levels const float MidIncome = 1000.00f; float FIT = 0.0f; // variable for income tax if (Gross >= HiIncome) { // Determine FIT amount. FIT = HiTax*Gross; } else if (Gross >= MidIncome) { FIT = MidTax*Gross; } else { FIT = LoTax*Gross; } return FIT; } ///////////////////////////////////////////////////////////////////// // Function to print header for output file. // Parameter: // outPay input file stream variable // // The function does need to modify this stream (by inserting to // it), so the parameter must be passed by reference (with an // ampersand). // // The function doesn't need to return a value, so it's void. // void PrintHeader(ofstream& outPay) { outPay << "Bill McQuain" << endl; outPay << "Macro$oft Corporation March Payroll" << endl << endl; outPay << " IdNum" << " Gross Pay" << " Ins."; outPay << " F.I.T." << " SSI"; outPay << " Net Pay" << endl; outPay << "======" << "============" << "=========="; outPay << "==========" << "========" << "==============" << endl; } ///////////////////////////////////////////////////////////////////// // Function to print a line of employee data to output file. // Parameter: // outPay input file stream variable // IdNum id number of employee // GrossPay gross pay for employee // InsFee insurance fee for employee // FIT income tax for employee // SSI social security tax for employee // NetPay net pay for employee // // The function does need to modify this stream (by inserting to // it), so the parameter must be passed by reference (with an // ampersand). // // The function doesn't need to return a value, so it's void. // void PrintLine(ofstream& outPay, int IdNum, float Gross, float Fee, float FIT, float SSI, float Net) { outPay << setw( 6) << IdNum; outPay << setw(12) << setprecision(2) << Gross; outPay << setw(10) << setprecision(2) << Fee; outPay << setw(10) << setprecision(2) << FIT; outPay << setw(10) << setprecision(2) << SSI; outPay << setw(12) << setprecision(2) << Net; outPay << endl; } ///////////////////////////////////////////////////////////////////// // Function to print final averages to input file. // Parameter: // outPay input file stream variable // TotalGross total gross pay (for all employees) // TotalIns total insurance fees // TotalFIT total income tax // TotalSSI total social security tax // TotalNet total net pay // NumEmployees total number of employees // // The function does need to modify this stream (by inserting to // it), so the parameter must be passed by reference (with an // ampersand). // // The function doesn't need to return a value, so it's void. // void PrintAvg(ofstream& outPay, float TotalGross, float TotalIns, float TotalFIT, float TotalSSI, float TotalNet, int NumEmployees) { // Print out footer for table: outPay << "======" << "============" << "=========="; outPay << "==========" << "========" << "==============" << endl; if (NumEmployees) { outPay << " Avg:"; outPay << setw(12) << setprecision(2) << TotalGross/NumEmployees; outPay << setw(10) << setprecision(2) << TotalIns/NumEmployees; outPay << setw(10) << setprecision(2) << TotalFIT/NumEmployees; outPay << setw(10) << setprecision(2) << TotalSSI/NumEmployees; outPay << setw(12) << setprecision(2) << TotalNet/NumEmployees; outPay << endl; } }