# Thread: Trouble with math operations-Beginner program

1. ## Trouble with math operations-Beginner program

I have been building a very basic program to convert dollars and change to coins. This program is similar to an example in the book I am using. I try to make small changes to ensure that I understand the concepts of the program. I am having success at converting the input of dollars and cents into all cents, and the proper number of quarters displays. Regardless of input, quarters is correct, dimes shows 1, nickels and pennies produces 0. Looking at the book example (I tried to build before cheating with example) my code appears accurate but the results do not work. I have compiled the code in Visual Studio 2010 and CodeBlocks v 10.05.

Code:
#include <iostream>
//This program makes change in quarters, dimes, nickles, and pennies
using namespace std;
int main()
{
int cents, dollars, total, quarters, dimes, nickels, pennies; //named variables
quarters = 25, dimes = 10, nickels = 5, pennies = 1;          //establishing vaLues for variables
cout << "This program will report change due/n"
<< "using the minimum number of coins." << endl;
cout << "Enter number of dollars. " << endl;
cin >> dollars;                                              //user input
cin.ignore();
cout << "You entered " << dollars << "."                    //output of user input
<< " Now enter the amount of change." << endl;         //user input
cin >> cents;
cin.ignore();
cout << "You entered " << cents << "." << endl;            //output of user input
total = dollars * 100 + cents;                             //converts dollars to cents and adds cents
cout << "Your total is " << total << "cents" << endl;	//total of input converted to cents
total = total / quarters;                               //divide by 25 to determine # of quarters
cout << "You receive" << total << "quarters." << endl; //output of quarters
total = total % quarters;                              //remainder of cents
total = total / dimes;                                 //divide by 10 to determine # of dimes
cout << "You receive" << total << "dimes." << endl;    //output of dimes
total = total % dimes;                                 //remainder of cents
total = total / nickels;                               //divide by 5 to determine # of nickels
cout << "You receive" << total << "nickels." << endl;  //output of nickels
total = total % nickels;                               //remainder of cents
total = total / pennies;                               //divide by 1 to determine # of pennies
cout << "You receive" << total << "pennies." << endl;  //output of pennies
cout << "Press the Enter key to close." << endl;       //keeps program open to view until Enter key is pressed
cin.get();
return 0;
}
I would appreciate a clue as to where I am making my code error(s).
Thank you

2. Your arithmetic is incorrect. Let's say that your input is 186 cents which is \$1.86. When you calculate the quarters you say total = 186/25 which is 7. You are confusing print statement values with remainder values that you need to continue computing the change in lower denominations.

3. Thank you claudiu, but i still do not understand my error. I changed int for quarters, dimes, nickels, and pennies to const int so they always retain their value.
total = total % dimes (for example) should put the value of the remainder after division into total, correct? Do I misunderstand the use of % ?

4. Suggestion: variables and constants named thusly:

Code:
const int CENTS_PER_QUARTER = 25;
const int CENTS_PER_DIME = 10;
const int CENTS_PER_NICKEL = 5;

int num_of_quarters = 0;
int num_of_dimes = 0;
int num_of_nickels = 0;
Maybe using these variable names will make it easier to figure out what you're doing.

5. Asking them to enter the number of dollars, and then assuming they are going to enter "1.86" is a little weird. I never talk about having 1.86 dollars. You might consider asking them to enter the dollars and cents as two different values. It just seems unnatural to refer to it the way you are.

Quzah.

6. Originally Posted by quzah
Asking them to enter the number of dollars, and then assuming they are going to enter "1.86" is a little weird. I never talk about having 1.86 dollars. You might consider asking them to enter the dollars and cents as two different values. It just seems unnatural to refer to it the way you are.
Quzah.
Originally Posted by OP
Code:
cout << "Enter number of dollars. " << endl;
cin >> dollars;                                              //user input
cin.ignore();
cout << "You entered " << dollars << "."                    //output of user input
<< " Now enter the amount of change." << endl;         //user input
cin >> cents;
cin.ignore();
cout << "You entered " << cents << "." << endl;            //output of user input
total = dollars * 100 + cents;                             //converts dollars to cents and adds cents
You mean what he is doing?

7. Thank you all for helping me solve my math operations dilemma. I now need to add an if statement because the program crashes if any of the operations tries to divide by 0.
This is fun and frustrating at the same time hehe.

8. Yes you've understood the modulo (%) operator correctly.

However, say you need to decide the coinage to return for an input of 137 cents, for example.

The "total" begins as 137.

You are taking 137/25 = 5 and then storing it in total. This is incorrect.

Total should only be updated in the lines which are returning the remainder.

9. Yes Ocifer, adding more variables allowed me to make things work. As a beginner my code is probably bulkier than it has to be, but I am determined to learn so neater code will surely come when I have a better understanding of the capabilities of the language. I am open to constructive criticism...I may not understand what is suggested but I am open to it. Here is what I have come up with so far.
Code:
#include <iostream>

using namespace std;
const int centsPerQuarter = 25;  //naming constant integers
const int centsPerDime = 10;     //constants represent coin values
const int centsPerNickel = 5;

int main()
{
int numOfQuarters = 0;
int numOfDimes = 0;
int numOfNickels = 0;
int dollars = 0;
int cents = 0;
int total = 0;
cout << "This program will report change due"
<< "using the minimum number of coins." << endl;
cout << "Enter number of dollars. " << endl;
cin >> dollars;                                              //user input
cin.ignore();
cout << "You entered " << dollars << "."                    //output of user input
<< " Next, enter the amount of change." << endl;         //user input
cin >> cents;
cin.ignore();
cout << "You entered " << cents << "." << endl;            //output of user input
total = dollars * 100 + cents;                             //converts dollars to cents and adds cents
cout << "Your total is " << total << "cents" << endl;	//total of input converted to cents
if (total > 25){
numOfQuarters = total / centsPerQuarter;
cout << "You receive " << numOfQuarters << " quarters." << endl;
total = total % (numOfQuarters * centsPerQuarter);      //calculating remainder of cents
} if (total > 10) {
numOfDimes = total / centsPerDime;
cout << "You receive " << numOfDimes << " dimes." << endl;
total = total % (numOfDimes * centsPerDime);
} else cout << "You receive 0 dimes" << endl;
if (total > 5) {
numOfNickels = total / centsPerNickel;
cout << "You receive " << numOfNickels << " nickels." << endl;
total = total % (numOfNickels * centsPerNickel);
} else cout << "You receive 0 nickels." << endl;
if (total < 1) { cout<< "You receive 0 pennies." << endl; }
else {
}
cout << "You receive " << total << " pennies." << endl;
cin.get();
return 0;
}

10. Not sure if everyone will agree with this suggestion, but here goes:

This program is quite specific.

In areas that use the dollar / cent construct of money, a quarter is always 25 cents, a dime 10 cents, a nickel 5 cents. I would do away with the constant variables altogether, and hard code the 25, 10, 5 in the program. It would look less cluttered, and then you'd only need a set of variables to store the actual coin quantities.

Caveat:

In general it is good to have constants declared if you're using them a lot, and then you need only change them once. Doing so will make more volatile programs easier to maintain, so my suggestion is only for this case.

You've only got three variables here, and I don't forsee a quarter (for whatever reason) to be redifined to something other than 25 cents. Sure 25 cents might be worth more or less on a given day, but it is a safe assumption that a quarter will be 25 cents out of 100. Same for dimes 10 out of 100.

Funnily enough, when i googled "decimal currency" this came up currency - Are all modern currencies based on decimals? - Stack Overflow

11. Originally Posted by Ocifer
Not sure if everyone will agree with this suggestion, but here goes:

This program is quite specific.

In areas that use the dollar / cent construct of money, a quarter is always 25 cents, a dime 10 cents, a nickel 5 cents. I would do away with the constant variables altogether, and hard code the 25, 10, 5 in the program. It would look less cluttered, and then you'd only need a set of variables to store the actual coin quantities.
No, this is called bad programming practice. Even if those values will not change very often, 25, 10, 5 are not very specific. They tell very little. Constants tell a lot more.
That is why the general advice is "avoid magic numbers."

12. You can't avoid named constants because you're used to a concept. That's assuming a lot about people and what they know, and that's why magic numbers are bad. I once did a similar program where I converted old GBP (before decimalization) into its current decimal form for school. I'll be damned if I remembered now what I wrote then.