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.
#include<iostream>
usingnamespace std;
int main(void)
{
cout <<"\n\t\tWelcome to the Converter Calculation Propgram!\n";
unsignedshort 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";
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.
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.
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
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"
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?
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?
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.