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
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
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.