How to use modulo????

Sep 20, 2015 at 11:15pm
I am very new to programming and I'm struggling with understanding modulo. I get that it means remainder, but I am having trouble using it in an actual program. Here is a ounces to pounds converter. the goal is to input a number of ounces and convert that to pounds with a remainder of ounces.

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

#include<iostream>

using namespace std;

int main(void)

    {
        
        cout <<"\n\t\tWelcome to the Converter Calculation Propgram!\n";
        
               unsigned short ounces, pounds, ounces_left;
        
               const ounces_per_pound = 16;
            
        cout<<"\nHow many ounces do you have?";
        
                cin>> ounces;
                
                cin.ignore(INT_MAX, '\n');
                
        cout<<"\nThank you!";
            
        cout<<"\nCalculating...\n";
            
            
                   // How do I setup the equation here???
            
        cout<<"\nDone.\n";
            
            
Last edited on Sep 20, 2015 at 11:17pm
Sep 20, 2015 at 11:28pm
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
#include<iostream>

using namespace std;

const int OUNCES_PER_POUND = 16;

int main( void )
{
    unsigned int ounces, pounds, ounces_left;
    cout << "Welcome to the Converter Calculation Program!" << endl;
    cout << "How many ounces do you have? ";

    cin >> ounces;
    cin.ignore(999, '\n');
  
    cout << "Thank you!" << endl << "Calculating..." << endl;

    pounds = ounces / OUNCES_PER_POUND;
    ounces_left = ounces % OUNCES_PER_POUND;

    if( pounds == 1)
        cout << "You have " << pounds << " lb with "<< ounces_left << " ounces leftover." << endl;
    else
        cout << "You have " << pounds << " lbs with "<< ounces_left << " ounces leftover." << endl;
}


Some explanation on the code:

Line 18 - since we're using integer type variables, integer division will not have decimals, aka floats, doubles, long, etc. This gives us a nice whole number to work with when we're trying to figure out how many pounds we have.

Line 19 - modular arithmetic is basically what we all learned in elementary school. Just take out groups of x from a number y, and see what's left over. For example, if I have 33 oz. How many groups of 16 are in 33? Only 2. So you do 23 - 16*2 = 1. And there is your remainder.
Last edited on Sep 20, 2015 at 11:38pm
Sep 21, 2015 at 12:08am
Ok that part makes a bit more sense now. What did you do in lines 21-24? I didn't learn much about if else statements yet. Can you explain what is happening in those lines? What does it do for the program? what does the ( pounds == 1 ) do exactly? etc.
Sep 21, 2015 at 12:19am
That's a preference thing. Because when I wrote it up earlier, I was testing it with a few numbers and the first number I typed in was 23. What printed out was "You have 1 lbs with 7 ounces leftover". Grammatically, that's incorrect. So I added an if else to check for when the # of pounds equaled 1.

If it's still kind of confusing for you, think about it this way:
How would you say $0? What about $1? $100? $5? Mind you, this is in English, not another language.

Also in C++ you're gonna have to distinguish between = and ==.

= is assignment. That is, you're giving a variable an exact value.
Ex. int a; a = 15; // you have now given variable a the value of 15;

== is equality. It return a boolean, meaning you HAVE to have two sides to compare.
Ex. int a; a = 15; int b; b = 20; /* b == a */ // this returns false, because 15 is NOT equal to 20
Last edited on Sep 21, 2015 at 12:20am
Sep 21, 2015 at 12:29am
ohhhhhh. Ok cool. This makes a lot more sense now. THANKS!!!!
Sep 21, 2015 at 12:46am
I tried compiling mine but I got some errors that I'm not sure how to fix. How is there no type in line 21 when I used const? and why do i need a ";" in the middle of a sentence in 38???

ERRORS:

convert.cpp: In function `int main()':
convert.cpp:21: error: ISO C++ forbids declaration of
`ounces_per_pound' with no type
convert.cpp:38: error: expected `;' before "ounces_left"
Last edited on Sep 21, 2015 at 5:01am
Sep 21, 2015 at 1:16am
Check your positions of your quotation marks "" in line 39.

Const is used to modify an object type, in this case, making your ounces_per_pound immutable. Now ask yourself this: what is the type of ounces_per_pound?
Sep 21, 2015 at 1:37am
Oh. I got it. I used const short and it worked. Thanks! Can you explain a bit more what const does exactly to the data type? What do you mean by "Const is used to modify an object type"? How does it modify it?
Sep 21, 2015 at 1:46am
There's several usages of const, some which will seem extremely confusing for you. But don't worry about it for now. Here's a good reference for you if you ever feel confused.

http://duramecho.com/ComputerInformation/WhyHowCppConst.html
Sep 21, 2015 at 1:57am
Hmm. Very interesting. and weird :/. Anyway thanks again for all the help and clarification!
Topic archived. No new replies allowed.