If your paintLitres has a decimal of less than 0.5 it's going to round "down" (really just truncate off the decimal). Try using the ceil() function ( #include <cmath> ).
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
Yeah, I get the idea. You can emulate ceil() by casting paintLitres to an int and subtracting that value from the paintLitres value. If that difference is greater than zero litresPaintReq = (int) paintLitres +1; .
Since this involves floating point and "0" becomes somewhat subjective, you should set a threshold for comparison so say if the difference between paintLitres and (int) paintLitres is greater than 0.001 then round up (since you can probably give or take a ml of paint).
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
It really isn't possible to cover every function/construct in the language in a single semester. I'm sure your instructor will not balk at the use of ceil(). A big part of programming classes is researching the language by digging into documentation and experimenting with new constructs.
Unless they specifically say to not use a construct, it's fair game. Use of a different construct shows effort and instructors like that.
A hand-trace seems to indicate that the method works, but I only did it once. There may be a situation under which it doesn't work.
Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
Well it's more than it needs to be:
if (paintLitres - (int) paintLitres > 0.001)
litresPaintReq = (int) paintLitres+1;
else
litresPaintReq = (int) paintLitres;
You'll go back and look at yours 6 months from now and have to scratch your head for a minute.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581