C++ coding help

So what i'm trying to do is write a program that prompts the user to enter the number of doodles to be shipped then calculates the number of each size container needed for the shipment. I must use a mod operator for the calculations.

My results aren't coming through correct, what am I doing wrong?



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
50
51
52
53
54
55
56
57
58
59
60
  #include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
 
using namespace std;
 
int main( )
{
 
const int huge_box = 50;
const int large_box = 25;
const int medium_box = 10;
const int small_box = 1;
int doodles;
int huge_doodle;
int large_doodle;
int medium_doodle;
int small_doodle;
 
cout  << "Enter the number of doodles to be shipped: ";
cin   >> doodles;
cout  << endl;
 
cout << "For shipment of " << doodles << " doodles: " << endl << endl;
cout << "Container        Number" << endl;
cout << "-----------------------" << endl;
 
 
huge_doodle = doodles % huge_box;
large_doodle = doodles % large_box;
medium_doodle = doodles % medium_box;
small_doodle = doodles % small_box;
 
 
cout << "Huge" << setw(18) << huge_doodle << endl;
cout << "Large" << setw(17) << large_doodle << endl;
cout << "Medium" << setw(16) << medium_doodle << endl;
cout << "Small " << setw(16) << small_doodle << endl;
 
 
ofstream fout;
fout.open("Prog2_out.txt");
 
fout << "For shipment of " << doodles << " doodles:" << endl << endl;
fout << "Container          Number" << endl;
fout << "-------------------------" << endl;
fout << "Huge                  " << huge_doodle << endl;
fout << "Large                 " << large_doodle << endl;
fout << "Medium                " << medium_doodle << endl;
fout << "Small                 " << small_doodle << endl;
 
cout << endl << endl;
cout << "Program results have also been written to Prog2_out.txt." << endl;
 
fout.close();
 
system("pause>nul");
 
}
You "must use modulus". Does it mean that you are not allowed to use any other operator?

The modulus returns remainder. The result of doodles % huge_box are the doodles that are left over once you have filled as many huge boxes as possible.

PS. Note that 0 == doodles % 1. Always.
I believe I must use / for the first operation and then modulus for the rest. I just don't know how to set it up properly.
I think I understand what you're asking here, but this seems like a much more roundabout method of doing it, if that makes sense. For instance, I could see using inequality operators to determine the size you'll need for a certain amount of doodles (30 doodles, I assume the constant values are doodles allowed per box, which is a medium box) then using the modulus to get the amount of doodles left over, and repeating the last process for the next size box you'll need for doodle shipment (doodles % 25 = 5 doodles), but with what you're saying the instructions are, and looking at it, it seems... more complicated than it should be.

I guess I would set up a variable for each size box (huge, large, med, small), and one method could be:

1
2
3
4
5
6
huge = doodles / huge_box;
doodles %= huge_box;
large = doodles / large_box;
doodles %= large_box;
//until you get to the last
small = doodles;


Well, if it's for a school/university assignment, I'm afraid you still need to follow the instructions given, but if it were me, I'd try to contact the instructor to verify that's what you're to do. It's entirely possible either you didn't understand, or they didn't explain it properly (or I'm not understanding).

Sorry man. That's all I got, but I'm trying to be helpful.
Topic archived. No new replies allowed.