// Payroll98.cpp: A Simple Payroll Program // // You should substitute the correct information in the following // lines: // // Programmer: Bill McQuain // ID Number: 999-99-9999 // Compiler: Visual C++ version 5.0 // Platform: Pentium 200 / Windows NT // Date: August 13, 1998 // // Purpose of the program: // // This program reads information about an arbitrary number of // employees from an input file named "workers.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. // // The is the #include section. These statments tell the compiler // to read and use variables and functions declared in the specified // files. #include #include // Every C++ program must have a function named main. This is the // beginning of the definition of the main function for this // simple program. In this case the whole program consists of just // the main function --- usually programs involve more than one // function. // int main() { // This section is where named constants are declared and given // their values. We could just use the numbers themselves in the // code that follows, but the names make the code more readable; // the name SSRate carries meaning where the number 0.0675 doesn't. // const double HiTax = 0.33; // tax rates const double MidTax = 0.28; const double LoTax = 0.15; const double SSRate = 0.0683; const double HiPlanB = 250.00; // insurance fees const double MidPlanB = 160.00; const double LoPlanB = 110.00; const double HiPlanD = 400.00; const double MidPlanD = 220.00; const double LoPlanD = 140.00; const double HiIncome = 3000.00; // income levels const double MidIncome = 1000.00; const char PlanB = 'B'; // insurance plan types const char PlanD = 'D'; // This section is where the variables used in main() are declared. // Each variable has a type (integer, double, etc) but no particular // value at this point. The variable names like the constant names // used above are supposed to be meaningful, and each declared // variable is also given a short comment explaining what it will // be used for. // ifstream inPay; // input file stream ofstream outPay; // output file stream int IdNum, // employee ID number, Age, // age. NumEmployees; // number of employees. double GrossPay, // employee gross pay. NetPay, // net pay, SSI, // SS tax, FIT, // income tax, InsFee, // insurance fee.. TotalGross, // total of all gross pay, TotalNet, // net pay, TotalIns, // insurance fees, TotalSSI, // SS tax, TotalFIT; // income tax. char InsPlan; // employee insurance plan // This is the initialization section of main(). We must make sure that // every variable is given a value before it is used in a calculation // or printed. Some variables get their values by being read from the // input file, so they don't need to be initialized here. NumEmployees = 0; // Initialize counters and running totals TotalGross = 0.0; // to zero. TotalNet = 0.0; TotalIns = 0.0; TotalSSI = 0.0; TotalFIT = 0.0; // The following four statements prepare the input and output files // for use. Don't worry too much about what's going on here just yet, // we'll cover the details shortly. // inPay.open("workers.dat"); // open input and output files outPay.open("payroll.dat"); outPay.setf(ios::fixed, ios::floatfield); outPay.setf(ios::showpoint); // The following statments print the header info to output file. It's // rather cryptic at this point, but you'll see output is really very // simple in C++. outPay << "Bill McQuain" << endl; outPay << "Macro$oft Corporation Payroll" << endl << endl; outPay << " IdNum Gross Pay F.I.T. SSI Ins. Net Pay"; outPay << endl; outPay << "============================================================"; outPay << endl; // This statement tries to read first line of data from input file. If // something goes wrong, the variable inPay will be false (small lie) and // that will prevent us from going into the loop that follows and generating // nonsense. inPay >> IdNum >> Age >> GrossPay >> InsPlan; // What comes next is a while loop. The body of the loop gets // executed over and over again until the expression that follows // the while (inPay in this case) becomes false. Therefore, we'd // better be sure that inPay will eventually become false, // otherwise the program will continue to execute the body of this // loop forever. while (inPay) { // The first part of the loop body (down to the comment with // all the hyphens) processes the data that was just read in. NumEmployees++; // Count employees. TotalGross += GrossPay; // Update total gross pay. inPay.ignore(200, '\n'); // Skip to next line of input. // The switch statement tries to match the value of the selection // variable (InsPlan this time) with the given cases. When a // match is found, the statements that accompany that case are // executed. If no match is found, the default case is carried out. switch (InsPlan) { // Calculate insurance fees: case 'B':{if (Age <= 35) // InsFee = LoPlanB; // for Plan B employees else if (Age <= 65) InsFee = MidPlanB; else InsFee = HiPlanB; break; } case 'D':{if (Age <= 35) // for Plan D employees InsFee = LoPlanD; else if (Age <= 65) InsFee = MidPlanD; else InsFee = HiPlanD; break; } default :{outPay << "Employee " << setw(4) << IdNum; outPay << " has invalid insurance plan." << endl; InsFee = 0.0f; } } TotalIns += InsFee; // Update total insurance fees. if (GrossPay >= HiIncome) { // Determine FIT amount. FIT = HiTax * GrossPay; } else if (GrossPay >= MidIncome) { FIT = MidTax * GrossPay; } else { FIT = LoTax * GrossPay; } 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. // --------------------------------- End of Processing Section // This section of the loop body prints the results calculated // above into the output file. outPay << setw( 6) << IdNum; outPay << setw(12) << setprecision(2) << GrossPay; outPay << setw(10) << setprecision(2) << FIT; outPay << setw(10) << setprecision(2) << SSI; outPay << setw(10) << setprecision(2) << InsFee; outPay << setw(12) << setprecision(2) << NetPay; outPay << endl; // This statement tries to read the next line of input. This // is the last statement in the loop body, so the next thing // that will happen is that the loop control expression (inPay // in this case) is tested. inPay >> IdNum >> Age >> GrossPay >> InsPlan; } // End of while loop. // Once we've exited the loop, we're done reading and processing data for // new employees. It's time to sum things up. The following statements // print out the averages in a nice form. First print a line to mark // the end of the table body: outPay << "============================================================"; outPay << endl; // Now, calculate and print averages, if appropriate. We've got to be // careful not to calculate averages if there weren't any employees, so // this uses an if statement to be sure that's not the case before any // dividing is done. if (NumEmployees != 0) { outPay << " Avg:"; outPay << setw(12) << setprecision(2) << TotalGross/NumEmployees; outPay << setw(10) << setprecision(2) << TotalFIT/NumEmployees; outPay << setw(10) << setprecision(2) << TotalSSI/NumEmployees; outPay << setw(10) << setprecision(2) << TotalIns/NumEmployees; outPay << setw(12) << setprecision(2) << TotalNet/NumEmployees; outPay << endl; } // These two statements just close the input and output files; this // tells the operating system that we're done with them and (hopefully) // that the files are properly saved. inPay.close(); outPay.close(); // The return statement sends a value back to whoever called this // function. If you're using Visual C++, you may see this value // displayed in the window at the bottom of the screen when you run // the program. return NumEmployees; }