//		This program gives the number of boxes required for a number of figurines

#include <iostream>
using namespace std;

int main()
	unsigned boxcapacity;
	cout << " Enter the total amount of figurines each box can store: ";
	cin >> boxcapacity;
	unsigned figurines;
	cout << " Enter the total amount of figurines: ";
	cin >> figurines;
	int boxesrequired = (figurines / boxcapacity);
	cout << "The amount of boxes required is " << boxesrequired << endl;
	system ("pause");

Hi I can't find the solution for this problem I am having.

if 10 items can fit in a box and we have 1234 items, the answer must be 124. I keep getting 123.4 when i divide.

likewise if i could fit 100 items in a box and i have 1,000,000 then i should have 10000 boxes and not 10001.

how would I round all decimal points up to the whole number and not down, and how do I leave whole numbers the way they are?

I tried adding 1 to the box capacity but this makes 10000 become 10001.

7 Years
Discussion Span
Last Post by packetpirate
static_cast<int>(boxesrequired + 0.5);

We're not here to do your homework for you... you can figure out how to use that and why it works.


Logically thinking, it is a math problem. Fortunately, C/C++ has a built-in library catering for common mathematical needs. You may want to look into these two functions: floor() and ceil()

Both from the math.h library.


Chiwawa, floor and ceil don't account for if the number after the decimal is greater or lower than 5. The casting technique I mentioned above is more than adequate.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.