+ 1

# Paint costs question

Why does the last test fail if I don't cast the round() in my final output to int ? ==== #include <iostream> #include <cmath> int main() { int initialCost = 40; int colorCost = 5; float tax = 1.1; int colorAmount; std::cin >> colorAmount; int total = initialCost; for(int i = 0; i < colorAmount; i++){ total += colorCost; } std::cout << (int) round(total * tax) << std::endl; return 0; }

7 Antworten

+ 3

Completely correct Lothar !

+ 2

The task says to "round to the nearest whole number", so if you don't do that, you aren't doing the task.

+ 2

This was the very same issue I encountered on this Paint Cost problem. Using ceil() will still have a return type of double though it might not be visible in the output. Using ceil and casting it to int or implicitly convert it to int will return a whole integer and truncate the decimals.

+ 1

You are getting ready to paint a piece of art. The canvas and brushes that you want to use will cost 40.00. Each color of paint that you buy is an additional 5.00. Determine how much money you will need based on the number of colors that you want to buy if tax at this store is 10%.
Task
Given the total number of colors of paint that you need, calculate and output the total cost of your project rounded up to the nearest whole number.
Input Format
An integer that represents the number of colors that you want to purchase for your project.
Output Format
A number that represents the cost of your purchase rounded up to the nearest whole number.
Sample Input
10
Sample Output
99

+ 1

Yes, use the ceil() to round up and cast it to int.

0

Ausgrindtube So instead of calling round() I'm calling std::ceil() now, which would round the result, as the task says, up to the nearest whole number. Yet without the typecast to int the last task and only the last task still fails. Why is that?

0

Pat 4/5 tasks succeed without the cast to int. What could make the last one different?
The two visible tasks show me a whole number as output even without the cast.