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; }
5 Réponses
+ 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.
+ 2
Stef , change the line where you print the result to => cout<<int(round(purchaseCosts))<<endl;
+ 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 🐱
0
Thanks! It worked.
0
Why is ceil not working in this case?