+ 1

Where is the bug?

#include <iostream> #include<cmath> using namespace std; int main() { int color; float fixed = 40.0; cin >> color; float cost = (5.0 * color) + fixed; cost += cost * 0.10; cout << ceil(cost); return 0; }

27th Jun 2025, 3:16 AM
Md Mehedi Hasan
Md Mehedi Hasan - avatar
2 odpowiedzi
+ 3
Md Mehedi Hasan I added explicit int conversion in the cout statement as: cout << (int)ceil(cost); And it worked. But I can't really understand what the issue is. The result normally returns an integer anyway(and so it passes 4 of the 5 test cases). The error is, to my understanding, due to the cost being a certain floating number, that when converted with ceil, rounds up to a number it shouldn't. Maybe because of floating point precision, but I'm not sure either. I did check by using double instead of float, but that didn't fix the issue like (int) conversion did.
27th Jun 2025, 4:36 AM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
+ 1
If you're using an online compiler or testing, the program does not prompt the user, so it may appear like it does nothing. #include <iostream> #include <cmath> using namespace std; int main() { int color; float fixed = 40.0; cout << "Enter number of colors: "; cin >> color; float cost = (5.0 * color) + fixed; cost += cost * 0.10; cout << "Total cost: " << ceil(cost) << endl; return 0; } Try it
27th Jun 2025, 11:49 AM
Chitranshi Gupta
Chitranshi Gupta - avatar