# Beginner Needing Help making change

• 03-21-2012
jp1978
Beginner Needing Help making change
This is my code so far:

Code:

```#include <iostream> using namespace std; int main(){ int cent; int quarter = 25; int dime = 10; int nickel = 5; int penny = 1; cout << "Enter an amount in cents: "; cin >> cent; cout << "The amount you entered is: " << cent << " cents\n"; cout << endl; //This is where I am getting lost if (cent % quarter) cout << "Quarters: " << cent << endl; if (cent % dime) cout << "Dimes: " << cent << endl; if (cent % nickel) cout << "Nickels: " << cent << endl; if (cent % penny) cout << "Pennies: " << cent << endl; system("pause"); return 0; }```
I have been having a lot of trouble with this one. I am trying to prompt the user for an amount of change, then determine how many quarters, nickels, dimes, and pennies the entered amount would make. I may have been staring at this for too long the longer I look at this the worse my code becomes. Any help would be greatly appreciated..
• 03-21-2012
jp1978
doh I was wayyyy over thinking this thing
Code:

```#include <iostream> using namespace std; int main(){ int cent; int quarter = 25; int dime = 10; int nickel = 5; int penny = 1; int solveQ; int solveD; int solveN; int solveP; int remainQ; int remainD; int remainN; cout << "Enter an amount in cents: "; cin >> cent; cout << endl; cout << "The amount you entered is: " << cent << " cents\n"; cout << endl; solveQ = cent / quarter;        //Finds # of Quarters     remainQ = cent % quarter;  //Finds remaining cents solveD = remainQ / dime;        //Finds # of Dimes     remainD = remainQ % dime;  //Finds remaining cents solveN = remainD / nickel;      //Finds # of Nickels     remainN = remainD % nickel; //Finds remaining cents solveP = remainD / penny;      //Finds # of Pennies cout << solveQ << " Quarter(s)" << endl; cout << solveD << " Dime(s)" << endl; cout << solveN << " Nickel(s)" << endl; cout << solveP << " Penny(s)\n" << endl; system("pause"); return 0; }```
It's not 100% right but I believe I am on the right track now
• 03-21-2012
GReaper
You got the concept wrong. When you try to break something down into smaller parts, you start from the proportionaly largest unit, divide your value by that unit and the quotent is the amount of units needed. The remainer is the one that should be repeatedly checked as the new value, but each time taking into account smaller units until it can't be further broken.

EDIT: Nice, that's better.
• 03-21-2012
jp1978
solveP = remainD / penny; //Finds # of Pennies is the problem / Solved thanx anyway
• 03-21-2012
jp1978
Whew my head was not in the right place... but thanx much Reaper
• 03-21-2012
whiteflags
Dividing by one doesn't change the answer.
• 03-22-2012
Elysia
Does it hurt to ask that you indent your code in the future?
• 03-22-2012
johndeaton
Here is another way to do it:

Code:

``` #include <iostream> using namespace std; typedef struct {     int quarters;     int dimes;     int nickels;     int pennies; } change; change count_change(int amount); int main() {     int amount;     change change1;     cout << "How much money do you have (in pennies)? ";     cin >> amount;     cin.ignore();     change1 = count_change(amount);     cout << "You have " << change1.quarters << " quarters, " << change1.dimes << " dimes, " << change1.nickels << " nickels, and " << change1.pennies << " pennies."; } change count_change(int amount) {     int quarters, dimes, nickels, pennies;     change change1;     quarters = amount / 25;     amount %= 25;     dimes = amount / 10;     amount %= 10;     nickels = amount / 5;     amount %= 5;     pennies = amount;     change1.quarters = quarters;     change1.dimes = dimes;     change1.nickels = nickels;     change1.pennies = pennies;     return change1; }```
• 03-22-2012
ferfy
By any chance are you reading "Practical C++ Programming"? Cuz that's an exercize in chapter 6...... the chapter i just finished :)