Weekly Pay roll
Oct 19, 2016 at 1:17am UTC
My problem arises when hours or pay rate is 0 as an input but the program does not calculate it as a 0.
I tried inputting 0 as the hours on the first 3 lines of code but after those few line the gross pay is not 0 when it is supposed to be.
this is the input:
123456789 25.5 3 40.0
234567812 15.5 4 45.0
234512345 16.3 0 60.0
123456799 15.5 3 40.0
213456782 25.5 5 50.5
123452345 17.5 2 35.0
123456321 -10.5 2 24.5
213345657 10.5 -2 45.0
123533212 17.2 3 -25.0
423556578 19.5 20 65.0
555555125 30.0 3 35.5
293049506 15.0 2 0.0
938950677 0.0 0 0.0
766445667 25.5 10 40.0
432123567 20.5 7 25.5
And this is the output
The gross pay is 736.25
The gross pay is 1828.35
The gross pay is 2448.35
The gross pay is 3133.73
The gross pay is 3126.23
ERROR
ERROR
ERROR
The gross pay is 3438.12
The gross pay is 3890.62
The gross pay is 3845.62
The gross pay is 3348.38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <iostream>
#include <fstream>
#include <conio.h>
#include <iomanip>
using namespace std;
#define in_file "paydata.txt"
#define out_file "result.txt"
int main()
{
ifstream ins;
ofstream outs;
long int social_insurance;
float pay_rate, hours, gross_pay = 0, gross_pay1 = 0, gross_pay2 = 0, gross_pay3 = 0, Net_Pay, pension;
int exemptions;
ins.open(in_file);
outs.open(out_file);
//Formating at the top
outs << "Brooks Leather Company " << endl;
outs << "Social insurance no." << setw(15) << "Gross Pay" << setw(15) << "Net Pay" ;
outs << setw(15) << "Pension" << setw(15) << "Deductions" << endl;
ins >> social_insurance >> pay_rate >> exemptions >> hours;
while (!ins.eof()){
ins >> social_insurance >> pay_rate >> exemptions >> hours;
//Error Check
if ( pay_rate < 0 || exemptions < 0 || hours < 0){
outs << "ERROR" << endl;
continue ;
}
if (pay_rate == 0.0 || hours == 0.0){
gross_pay = 0;
}
//Find the gross pay
else {
if (hours <= 40){
gross_pay1 = hours * pay_rate;
}
if (hours > 40 && hours < 54){
gross_pay2 = (40 * pay_rate) + ((hours - 40)* (pay_rate * 1.5));
}
if (hours >= 54){
gross_pay3 = (hours - 54)* pay_rate+ ((40 * pay_rate) + (14 * (pay_rate * 1.5)));
}
gross_pay = gross_pay1 + gross_pay2 + gross_pay3;
outs << "The gross pay is " << gross_pay << endl;
float taxablepay = gross_pay - 14.00 * exemptions - 11.00;
float federal = taxablepay * (14.00 + 0.00023 * taxablepay);
float provincial = federal * 0.35;
pension = gross_pay * 0.077;
if (pension < 16.50){
Net_Pay = taxablepay - federal - provincial + 16.50;
}
else {
Net_Pay = taxablepay - federal - provincial + pension;
}
}
}
ins.close();
outs.close();
_getch();
}
Oct 19, 2016 at 2:04am UTC
Program works fine. You just don't write to outfile when gross pay is zero.
1 2 3 4 5 6 7 8 9 10 11
...
if (pay_rate == 0.0 || hours == 0.0){
gross_pay = 0;
outs << "The gross pay is " << gross_pay << endl;
}
//Find the gross pay
else {
gross_pay1 = 0; gross_pay2 = 0; gross_pay3 = 0; // should set to zero each time
if (hours <= 40){
...
Also gross pay for first line in infile is never calculated.
Last edited on Oct 19, 2016 at 2:06am UTC
Topic archived. No new replies allowed.