Let me introduce myself: my name is Monica, and I am taking my first computer science class.
The assignment my teacher wants us to complete: We're editing one of our previous programs that chooses, out of two possible payment plans, which plan is better, and then calculates a "bicycle salesperson's weekly salary", and outputs it to the screen. In the original program the user inputed the values (amount of hours worked, total weekly sales, and number of dependents). We're supposed to edit this program by having the computer retrieve the values from an input file, which is supposed to have a few lines of values. To make sure the program reads and calculates all possible sets of values, we have to put in a while loop. The information is supposed to output to a file as well.
My problem: For some reason the last line in the input file is outputted twice.
What my input file looks like:
CODE
40 1200 4
30 1200 0
What my output file looks like:
CODE
Gross Health Social State Federal Union Net
Pay Insurance Security Income Tax Income Tax Dues Pay
----- ---------- -------- ---------- ---------- ----- ---
450.00 10.00 27.00 22.50 63.00 6.00 321.50
Gross Health Social State Federal Union Net
Pay Insurance Security Income Tax Income Tax Dues Pay
----- ---------- -------- ---------- ---------- ----- ---
333.00 10.00 19.98 16.65 46.62 6.00 233.75
Gross Health Social State Federal Union Net
Pay Insurance Security Income Tax Income Tax Dues Pay
----- ---------- -------- ---------- ---------- ----- ---
330.00 10.00 19.80 16.50 46.20 6.00 231.50
What my code looks like now:
(please note I have a lot of comments in my code right now to ensure I understand what I am doing)
CODE
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
/* Constants */
const double reg_hp_rate = 8.25; // Hourly rate if salesperson worked
// 40 hours or less.
const double ot_hp_rate = 12.375; // Hourly rate for overtime(ot) hours
const double com_rateA = 0.10; // Commission rate for Plan A
const double com_rateB = 0.25; // Commission rate for Plan B
const double soc_rate = 0.06; // Social security tax rate
const double fed_rate = 0.14; // Federal income tax rate
const double state_rate = 0.05; // State income tax rate
const double union_dues = 6.00; // Amount of money paid to the union
const double dep_duct = 10.00; // Amount of money deducted if person
// has 4 or more dependents.
const double reg_hours = 40; // After forty hours the person is paid
// at an overtime rate
/*-------------------Beginning of Main Function-------------------------------*/
int main()
{
/*~~~~~~~~~~~Declare variables, and open files~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* File input and output variables */
ofstream salary3Out;
ifstream salary3In;
/* Variables */
double varHours; // Number of hours worked reading
double varComA; // How much money earned from sales
double varWeeklySales; // Total amount of weekly sales
double varSocSecTax; // Total amount taken out for social
// social security tax
double varFedTax; // Total amount taken out for federal
// income tax
double varStateTax; // Total amount taken out for state
// income tax
double varTotalDed; // Total amount deducted
double varNetTh; // Net take home
double varDependents; // Number of dependents
double varPlanA_overtime; // Amount of overtime hours
double varNoOvertimeWage; // Wage if person did not work overtime
double varRegHourlyWage; // Part 1 of overall wage if person
// worked overtime. This part is
// calculated by the regular rate.
double varOvertimeHourlyWage; // Part 2 of overall wage if person
// worked overtime. This part is
// calculated by a higher rate than the
// regular rate.
double varOvertimeTotalHourlyWage; // The total hourly wage if a person
// worked overtime
double varPlanAGrossPay; // the gross pay for plan A
double varPlanBGrossPay; // the gross pay for plan B
/* Open output and input files */
salary3Out.open("salary3.out");
salary3In.open("salary3.in");
/*~~~~~~~~~~~~~~~~~~~~End of Declarations~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*~~~~~~~~~~~~~~~~Beginning of End of File While Loop~~~~~~~~~~~~~~~~~~~~~*/
salary3In >> varHours; // Primming Read
while (salary3In) // While data is still in salar3.in
{
// Step 1. Read amount of hours worked, total amount of weekly sales, and
// amount of dependents from input file.
salary3In >> varWeeklySales;
salary3In >> varDependents;
// Step 2. Calculate the gross pay for each plan.
/* Calculate the gross pay for Plan A */
varComA = com_rateA*varWeeklySales; // Calculate commission
// for plan A
varPlanA_overtime = varHours - 40; // Determine if person
// worked overtime
if(varPlanA_overtime <= 0) // If the person didn't
// work overtime
{
varNoOvertimeWage = reg_hp_rate*varHours;
varPlanAGrossPay = varNoOvertimeWage + varComA;
}
else // Else,if the person
// worked overtime
{
varRegHourlyWage = reg_hp_rate*reg_hours;
varOvertimeHourlyWage = ot_hp_rate*varPlanA_overtime;
varOvertimeTotalHourlyWage = varOvertimeHourlyWage +
varRegHourlyWage;
varPlanAGrossPay = varOvertimeTotalHourlyWage + varComA;
}
/* Calculate the gross pay for Plan B */
varPlanBGrossPay = com_rateB*varWeeklySales;
// Steps 3, 4, 5, and 6
/* Determine which plan pays better, and use better paying plan
to calculate deductions, net take home, and output
data */
if (varPlanAGrossPay > varPlanBGrossPay) // If plan A pays better
{
// Calculate amount deducted for social security tax,
// federal income tax, and state income tax.
varSocSecTax = varPlanAGrossPay*soc_rate;
varFedTax = varPlanAGrossPay*fed_rate;
varStateTax = varPlanAGrossPay*state_rate;
// Determine whether to deduct money for dependents,
// and output data.
if (varDependents >= 3) // Three or more dependents?
{
varTotalDed = varSocSecTax + varFedTax +
varStateTax + union_dues + dep_duct;
varNetTh = varPlanAGrossPay - varTotalDed;
// Output Data
salary3Out << fixed << setprecision(2) << "Gross"
<< setw(12) << "Health" <<
setw(9) << setw(9) << "Social" <<
setw(9) << "State" << setw(14) << "Federal" <<
setw(8) << "Union" << setw(7) << "Net\n";
salary3Out << setw(4) << "Pay" << setw(13) <<
"Insurance" << setw(10) << "Security" << setw(11)
<< "Income Tax" << setw(13) << "Income Tax" <<
setw(5) << "Dues" << setw(8) << "Pay\n";
salary3Out << "-----" << setw(12) << "----------"
<< setw(10) << "--------" << setw(11) <<
"----------" << setw(13) << "----------" <<
setw(6) << "-----" << setw(6) << "---";
salary3Out << endl;
salary3Out << varPlanAGrossPay <<
setw(3) << "" << dep_duct << setw(6)
<< " " << varSocSecTax << setw(5) << " " <<
varStateTax << setw(8) << " " << varFedTax <<
setw(4) << " " << union_dues << setw(4) << " "
<< varNetTh << endl;
}
else // Less than three dependents?
{
varTotalDed = varSocSecTax + varFedTax +
varStateTax + union_dues;
varNetTh = varPlanAGrossPay - varTotalDed;
// Output data
salary3Out << "Gross" << setw(12) << "Health" << setw(9) <<
setw(9) << "Social" << setw(9) << "State" << setw(14) <<
"Federal" << setw(8) << "Union" << setw(7) << "Net\n";
salary3Out << setw(4) << "Pay" << setw(13) << "Insurance" <<
setw(10) << "Security" << setw(11) << "Income Tax" << setw(13)
<< "Income Tax" << setw(5) << "Dues" << setw(8)
<< "Pay\n";
salary3Out << "-----" << setw(12) << "----------"
<< setw(10) <<
"--------" << setw(11) << "----------" << setw(13)
<< "----------" << setw(6) << "-----" << setw(6) << "---";
salary3Out << endl;
salary3Out << varPlanAGrossPay <<
setw(3) << "" << "0.00" << setw(6)
<< " " << varSocSecTax << setw(5) << " " << varStateTax <<
setw(8) << " " << varFedTax << setw(4) << " " << union_dues <<
setw(4) << " " << varNetTh << endl << endl;
}
}
else // If plan B pays better
{
// Calculate the amount deducted for social security tax, federal income
// tax, and state income tax
varSocSecTax = varPlanAGrossPay*soc_rate;
varFedTax = varPlanAGrossPay*fed_rate;
varStateTax = varPlanAGrossPay*state_rate;
// Determine whether to deduct money for dependents, and output data.
if(varDependents >= 3) // Three or more dependents?
{
varTotalDed = varSocSecTax + varFedTax + varStateTax +
union_dues + dep_duct;
varNetTh = varPlanBGrossPay - varTotalDed;
// Output data
salary3Out << "Gross" << setw(12) << "Health" << setw(9) << setw(9)
<< "Social" << setw(9) << "State" << setw(14) << "Federal" <<
setw(8) << "Union" << setw(7) << "Net\n";
salary3Out << setw(4) << "Pay" << setw(13) << "Insurance" <<
setw(10) << "Security" << setw(11) << "Income Tax" <<
setw(13) << "Income Tax" << setw(5) << "Dues" << setw(8)
<< "Pay\n";
salary3Out << "-----" << setw(12) << "----------"
<< setw(10) <<
"--------" << setw(11) << "----------" << setw(13)
<< "----------" << setw(6) << "-----" << setw(6) << "---";
salary3Out << endl;
salary3Out << varPlanAGrossPay <<
setw(3) << "" << dep_duct << setw(6)
<< " " << varSocSecTax << setw(5) << " " << varStateTax <<
setw(8) << " " << varFedTax << setw(4) << " " << union_dues
<< setw(4) << " "<< varNetTh << endl;
}
else // less than three dependents?
{
varTotalDed = varSocSecTax + varFedTax + varStateTax +
union_dues;
varNetTh = varPlanBGrossPay - varTotalDed;
// Output all data
salary3Out << "Gross" << setw(12) << "Health" << setw(9) <<
setw(9) << "Social" <<
setw(9) << "State" << setw(14) << "Federal" <<
setw(8) << "Union" << setw(7) << "Net\n";
salary3Out << setw(4) << "Pay" << setw(13) << "Insurance" <<
setw(10) << "Security" << setw(11) << "Income Tax" <<
setw(13) << "Income Tax" << setw(5) << "Dues" << setw(8)
<< "Pay\n";
salary3Out << "-----" << setw(12) << "----------"
<< setw(10) <<
"--------" << setw(11) << "----------" << setw(13)
<< "----------" << setw(6) << "-----" << setw(6) << "---";
salary3Out << endl;
salary3Out << varPlanAGrossPay <<
setw(3) << "" << "0.00" << setw(6)
<< " " << varSocSecTax << setw(5) << " " << varStateTax <<
setw(8)
<< " " << varFedTax << setw(4) << " " << union_dues <<
setw(4) << " "
<< varNetTh << endl;
}
}
}
/*---------------End of While Loop----------------------------------------*/
/* Three blank lines added for readabilty */
cout << "\n\n\n";
system("PAUSE");
return 0;
}
/*-------------------End of Main Function-------------------------------------*/
Thanks ahead of time for any help.
~Monica
This post has been edited by missme: 14 Mar, 2007 - 09:17 PM