help with c++ "if " program

Hey there, I am learning C++ and I need help to build a program the exercise: 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 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.
1- The user to enter the number of cookies (24 cookies equals 1 box and 75 boxes equal 1 container)
2- the program would show the number of boxes and containers needed
3- If the user has leftovers cookies the program should show the reminder with the message you have X else not show the expression " you have X cookies left". Thank you :)

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
  #include<iostream>
using namespace std;
int main()
{
int cookie;
const int box=cookie/24;
const int container=box/75;
float leftC;
int leftB;
int numC;
int numB;
cout<<"enter the number of cookies";
cin>>numC;
cout<<"the number of boxes needed to hold the cookies"<<endl;
box=numC/cookie;
cout<<box<<endl;
leftC=numC%box;
cout<<"leftover cookies = "" "<<leftC<<endl;
cout<<"the number of container needed to store the boxes"<<endl;
cout<<container<<endl;
leftB=box%container;
cout<<"leftover boxes =" <<leftB;

return 0;

}
Last edited on
Line 6, what is the value of cookie?

Lines 6, 7, 15 have integer division. That discards remainder.

Line 8 declares a float variable. Why?

If you can deliver only whole containers, then the number of delivered cookies is 24*75*K, where K is the number of containers.

The remainder (of cookies/(24*75) ) is the leftovers. It equals B*24+C, where B is number of leftover boxes and C the cookies over last box.


Your program has no if statements and it does not need them either, unless you want to print leftovers only when there are some.
I need to create a program with the "if" statement, that is what the teacher wants us to understand.

I think I got it this time

#include <iostream>

using namespace std;

int main()
{
int cookies, boxes,extraCookies, containers, extraBoxes;

cout<< "Please enter the total number of cookies:";
cin >> cookies;
cout<< "\nThe total number of cookies is:"<< cookies<< endl;

boxes = cookies / 24;
extraCookies = cookies % 24;
containers = boxes / 75;
extraBoxes = boxes % 75;


{
if (boxes > 0)
cout << "boxes needed = " << boxes << endl;
if (containers > 0)
cout << "containers needed = " << containers << endl;
if (extraBoxes > 0)
cout << "leftover boxes = " << extraBoxes << endl;
if (extraCookies > 0)
cout << "leftover cookies = " << extraCookies << endl;

}
return 0;
}
What's the point of the extra braces surrounding the if-statements?

1
2
3
4
5
{
  if (boxes > 0)
    cout << "boxes needed = " << boxes << endl;
  // ...
}


This isn't wrong, but this isn't a case where you would usually insert extra braces.
Topic archived. No new replies allowed.