set a flag for money value then unset for time

Im trying to set the flag for money but this function runs in a loop and the flag and precision stays so i try to un set it after its displays but the next time it runs theHoursWorked is rounded to a whole number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void printPaySlip (string theEmployeeP, float theHoursWorkedP, float thePayRateP, float thePayP)
{
    float overtime = 0;
    if (theHoursWorkedP > 40)
        overtime = theHoursWorkedP - 40;

    cout << "Pay slip for " << theEmployeeP << endl;
    cout << "Hours worked: " << theHoursWorkedP << " hours" << endl;
    cout << "Overtime hours: " << overtime << " hours" << endl;
    cout.precision(2);
    cout.setf(ios::fixed);
    cout << "Hourly pay rate: R" << thePayRateP << endl;
    cout << "Pay: R" << thePayP << endl << endl;
    cout.unsetf(ios::floatfield);
}

So move lines 10-11 to line 6. What's the problem?

If you care about formatting, you should never assume the status of the formatting flags on entry to your function. Your function should set the formatting flags it needs.

If you want to be well behaved, you can save the formatting flags on entry, set what you need, then restore the previous flags on exit.
If I do that it would affect the output of the time, I would like the time to be the default setting ie 45.6 hours or 20.5 hours and the Money must be R 205.68 or like R20082.05 as like the first output the default settings.

As you can see from the output below it rounds off the output for hours worked after the first loop.

Please enter the employees Name: Harry Matispe
Please enter the hours worked: 43.5
Please enter the hourly pay rate: R125.35

Pay slip for arry Matispe
Hours worked: 43.5 hours
Overtime hours: 3.5 hours
Hourly pay rate: R125.35
Pay: R5672.09

Please enter the employees Name: Ellen Malan
Please enter the hours worked: 39.4
Please enter the hourly pay rate: R112.75

Pay slip for Ellen Malan
Hours worked: 39 hours
Overtime hours: 0 hours
Hourly pay rate: R112.75
Pay: R4442.35

Please enter the employees Name: Joey Rashdien
Please enter the hours worked: 45.6
Please enter the hourly pay rate: R120.45

Pay slip for Joey Rashdien
Hours worked: 46 hours
Overtime hours: 5.6 hours
Hourly pay rate: R120.45
Pay: R5829.78

Please enter the employees Name: Mpho Bopape
Please enter the hours worked: 41.2
Please enter the hourly pay rate: R123.60

Pay slip for Mpho Bopape
Hours worked: 41 hours
Overtime hours: 1.2 hours
Hourly pay rate: R123.60
Pay: R5166.48

Please enter the employees Name: Veli Singh
Please enter the hours worked: 39.7
Please enter the hourly pay rate: R135.30

Pay slip for Veli Singh
Hours worked: 40 hours
Overtime hours: 0 hours
Hourly pay rate: R135.30
Pay: R5371.41
Last edited on
If you want hours to be a single decimal point, do a cout.precision(1) and remove the the cout.unsetf(ios::floatfield) at line 14.

I don't understand why you're having a problem setting the formatting you want BEFORE doing the cout. As I pointed out before it is a bad practice to assume the program has left the formatting flags in a particular state. What if you later add another print function that changes the formatting flags? You're now at the mercy of whatever changes that function made to the formatting flags.

The hours are getting truncated on subsequent output because at line 14 you're turning off ios::floatfield. So on the second and subsequent times through, hours is not treated as a floating point number.
I tried that but then it is displayed as scientific notation, why would the way I do it only affect the first hours worked and not overtime? Thats what is more confusing to me.


Please enter the hours worked: 45.6

Hours worked: 46 hours
Overtime hours: 5.6 hours


I do understand that its best practice to ensure your formatting is stipulated before but this is the first time Im needing to format two different things I like the default as if there is no period then it doesn't show it ie 45 displays 45 but if im forcing precision to 1 and set flag fixed it would always display the period ie 45.0 and that looks untidy for time but with money you want to always show the two digits after the period.
Last edited on
Topic archived. No new replies allowed.