Help with C++ question

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;
}
Last edited on
Some of your logic doesn't make much sense. You're making it much more complicated than it needs to be.


1
2
3
4
int numBoxesNeeded = numCookies / BOX_MAX;
int numCookieLeftOver = numCookies % BOX_MAX;
int numContainersNeeded = numBoxesNeeded / CONTAINER;
int numBoxesLeftOver = numBoxesNeeded % CONTAINER;


Sure, this is kind of just giving you the answer; make sure you understand how it works. You were doing it much more awkwardly. You had pieces of this already, but what leftoverCookies %= numBoxes; was meant to do I have no idea.
Last edited on
Yes I overthought it completely, thank you for your help. I do understand what that is doing, I am just still not 100% on what the "%" operator fully does. When I had done leftoverCookies %= numBoxes; I was intending to have it take the leftover cookies from numBoxes after it was done finding the number of boxes with
numBoxes = numCookies / BOX_MAX;
Last edited on
Here is a simple example of / and %:
1
2
3
4
5
6
7
#include <iostream>

int main() {
  for ( int x=0; x < 30; ++x ) {
    std::cout << '\t' << x << " \t" << x / 7 << " \t" << x % 7 << '\n';
  }
}
Topic archived. No new replies allowed.