Thread: Change making program prints correct values but prints them over and over.

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Change making program prints correct values but prints them over and over.

    I have a program that makes change from an amount. It works fine, but the output is the correct output looped over and over. I have tried everything, but it still doesn't work. For example, a amount of 98 should print
    3 quarters
    2 dimes
    0 nickles
    3 pennies
    but instead it prints
    3 quarters
    2 dimes
    0 nickels
    3 pennies
    0 quarters
    2 dimes
    0 nickels
    3 pennies
    0 quarters
    2 dimes
    0 nickels
    3 pennies
    Can someone figure out why it's doing this?
    Code:
    #include <iostream>
    using namespace std;
    int coinscount(int& amount, int value) {
        int tracker = 0;
        int amountdimes = amount;
        int trackdimes = 0;
        int amountnickles = amount;
        int tracknickles = 0;
        int amountpennies = amount;
            for(amount; amount > 24; amount-= 25) {
                        tracker++;
            }
            cout << tracker << " quarters";
                         cout << endl;
            for(amountdimes= amount % 25 ; amountdimes > 9; amountdimes-= 10) {
                         trackdimes++;
                         }
                         
                         cout << trackdimes << " dimes";
                         for(amountnickles = amountdimes % 10; amountnickles > 4; amountnickles-= 5) {
                                      tracknickles++;
                                      }
                                      
                                      
                                      amountpennies = amountnickles % 5;
            cout << endl;
            cout << tracknickles << " nickels";
            cout << endl;
            cout << amountpennies << " pennies";
            cout << endl;
            }
    int main ( ) {
        int amountmain;
        cout << "Enter an amount: ";
        cin >> amountmain;
        cout << endl;
       
        coinscount(amountmain, amountmain); 
      
      coinscount(amountmain, amountmain); 
    
    
      coinscount(amountmain, amountmain); 
      
    }
    Any help would be very appreciated.

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Any help?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,889
    For starters, you need to format your code properly, especially where indentation is concerned. For example:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int coinscount(int& amount, int value) {
        int tracker = 0;
        int amountdimes = amount;
        int trackdimes = 0;
        int amountnickles = amount;
        int tracknickles = 0;
        int amountpennies = amount;
        for (amount; amount > 24; amount-= 25) {
            tracker++;
        }
        cout << tracker << " quarters";
        cout << endl;
        for (amountdimes = amount % 25; amountdimes > 9; amountdimes -= 10) {
            trackdimes++;
        }
    
        cout << trackdimes << " dimes";
        for (amountnickles = amountdimes % 10; amountnickles > 4; amountnickles-= 5) {
            tracknickles++;
        }
    
        amountpennies = amountnickles % 5;
        cout << endl;
        cout << tracknickles << " nickels";
        cout << endl;
        cout << amountpennies << " pennies";
        cout << endl;
    }
    
    int main() {
        int amountmain;
        cout << "Enter an amount: ";
        cin >> amountmain;
        cout << endl;
    
        coinscount(amountmain, amountmain);
    
        coinscount(amountmain, amountmain);
    
        coinscount(amountmain, amountmain);
    }
    Your problem appears to be the direct result of calling coinscount three times instead of just once.

    EDIT:
    Oh wait, Kelton2, this issue about code formatting isn't for starters anymore. You have been told over and over and over again. I have provided you example after example. If you have problems understanding what good formatting looks like, or what are the reasonably accepted practices for indentation, please voice it out now.
    Last edited by laserlight; 01-27-2015 at 10:41 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by laserlight View Post

    Oh wait, Kelton2, this issue about code formatting isn't for starters anymore. You have been told over and over and over again. I have provided you example after example. If you have problems understanding what good formatting looks like, or what are the reasonably accepted practices for indentation, please voice it out now.
    On the other paw, if you are *toooo* lazy to do the formatting, you could just throw it at 'clang-format' and edit a few things thereafter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-13-2015, 09:03 AM
  2. Replies: 1
    Last Post: 09-12-2013, 11:18 PM
  3. Replies: 2
    Last Post: 09-06-2011, 01:08 AM
  4. Why the program prints line twice?
    By red463 in forum C Programming
    Replies: 15
    Last Post: 04-30-2010, 05:20 AM
  5. program that prints itself
    By modec in forum C Programming
    Replies: 1
    Last Post: 10-10-2003, 02:14 PM