Thread: Beginner Needing Help making change

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    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..

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    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

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    solveP = remainD / penny; //Finds # of Pennies is the problem / Solved thanx anyway

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    4
    Whew my head was not in the right place... but thanx much Reaper

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Dividing by one doesn't change the answer.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Does it hurt to ask that you indent your code in the future?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    12
    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;
    }

  9. #9
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    By any chance are you reading "Practical C++ Programming"? Cuz that's an exercize in chapter 6...... the chapter i just finished

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a change counting program
    By Scrogdor in forum C Programming
    Replies: 15
    Last Post: 02-23-2011, 01:00 PM
  2. Terrain Making Beginner
    By Boomba in forum Game Programming
    Replies: 7
    Last Post: 08-28-2005, 12:33 PM
  3. Beginner Needing Help
    By MadProfessor in forum C Programming
    Replies: 4
    Last Post: 08-08-2005, 04:31 PM
  4. Needing help please....
    By tameeyore in forum C Programming
    Replies: 2
    Last Post: 02-06-2005, 01:06 PM
  5. Beginner needing help in C language
    By chauncey005 in forum C Programming
    Replies: 2
    Last Post: 09-26-2003, 02:04 PM