Hi, as a homework assignment I have been asked to write code to meet the following criteria:
"A box of cookies can hold 24 cookies, and a container can hold 75 boxes of cookies.
Write a program that prompts the user to enter:
The total number of cookies
The program then outputs:
The number of boxes and the number of containers to ship the cookies.
Note that each box must contain the specified number of cookies, and each container must contain the specified number of boxes. If the last box of cookies contains less than the number of specified cookies, you can discard it and output the number of leftover cookies.
Similarly, if the last container contains less than the number of specified boxes, you can discard it and output the number of leftover boxes."
I feel that I am very close, but I keep having issues with the leftover cookies and boxes. If anyone could give their opinion that would be greatly appreciated, thank you.
Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
using namespace std;
int main() {
const int BOX_MAX = 24;
const int CONTAINER = 75;
int numCookies, numBoxes, numContainers, leftoverCookies, leftoverBoxes;
cout << "Please enter the number of cookies: " << endl;
cin >> numCookies;
cout << endl;
if (numCookies < BOX_MAX)
{
numBoxes = 0;
leftoverCookies %= numCookies;
}
else
{
numBoxes = numCookies / BOX_MAX;
if (numCookies < BOX_MAX)
{
numBoxes = 0;
leftoverCookies %= numCookies;
}
}
if (numBoxes < CONTAINER)
{
numContainers = 0;
leftoverBoxes %= numBoxes;
}
else
{
numContainers = numBoxes / CONTAINER;
if (numBoxes < CONTAINER)
{
numContainers = 0;
leftoverBoxes %= numBoxes;
}
}
cout << "Number of boxes: " << numBoxes << endl;
cout << "Leftover cookies: " << leftoverCookies << endl;
cout << "Number of containers: " << numContainers << endl;
cout << "Leftover boxes: " << leftoverBoxes << endl;
return 0;
}
|