0

Code Coach in C++: Paint costs

Hello everyone, What's wrong in my code? #include <iostream> #include <cmath> using namespace std; int main() { int colors ; cin>>colors; float tax = (40.00+colors*5.00)*0.1; float purchaseCosts = 40.00+colors*5.00+tax; cout<<ceil(purchaseCosts)<<endl; return 0; }

15th Apr 2020, 11:43 AM
Steve
5 Respostas
+ 3
ceil() would have worked as well (and is technically more correct because round() doesn't always round up, but that doesn't matter here since the result either ends with .0 or .5), there seems to be a problem with printing the result as a float in this challenge (I think nobody really knows why, must be something related to floating point precision), that is why converting the result to integer fixes it. Others had the same issue as well.
15th Apr 2020, 1:32 PM
Shadow
Shadow - avatar
+ 2
Stef , change the line where you print the result to => cout<<int(round(purchaseCosts))<<endl;
15th Apr 2020, 11:55 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Stef , if I follow the task description it says "rounded up to the whole nearest integer", nearest for me means round the result and then convert it to int 🐱
15th Apr 2020, 12:35 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
Thanks! It worked.
15th Apr 2020, 12:15 PM
Steve
0
Why is ceil not working in this case?
15th Apr 2020, 12:16 PM
Steve