Coin Counting Program Problems

This is a discussion on Coin Counting Program Problems within the C Programming forums, part of the General Programming Boards category; New to the forum, and was hoping I could get some help. I'm trying to teach myself my university's introduction ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    2

    Coin Counting Program Problems

    New to the forum, and was hoping I could get some help. I'm trying to teach myself my university's introduction to computer science class over the summer so I would be better prepared when I take it for real. One of the programs is to apply a "greedy" approach to coin counting. For example:

    If I have $0.42, I would realize that it can only contain one quarter, giving me $0.16 left over. That would correlate to one dime with $0.06 left over. That would result in one nickel and two penny resultantly. The output of the program would be "5," representing the least amount of coins possible to make $0.42.

    So, here is the portion of my attempt that didn't work

    Code:
       // analyzes user input
        int quarters = 0;
        int dimes = 0;
        int nickels = 0;
        int pennies = 0;
    
        if (change2 >= 25)
            do
            {
            quarters = change2 / 25;
            change2 = change2 % 25;
            }
            while (change2 >= 25);
    
        if (change2 < 25 || change2 >= 10)
            do
            {
            dimes = change2 / 10;
            change2 = change2 % 10;
            }
            while (change2 < 25 || change2 >= 10);
    
        int total = quarters + dimes + nickels + pennies;
        printf("%d\n", total);
        printf("%d\n", change2);
    }
    


    I have only included the first two steps, because that's where the program begins to break down. The first section with quarters works fine. The problem is getting the data to transfer to the section on dimes. It just seems not to work. Even when I isolate the dime section, the second "if," it produces nothing. There should be no syntax errors involved.

    Thanks so much!
    Last edited by kevinandrewsun; 06-02-2012 at 10:18 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    What does this do - without any of your if / while statements?

    Code:
            quarters = change2 / 25;
            change2 = change2 % 25;
            dimes = change2 / 10;
            change2 = change2 % 10;
    Oh, when you paste code, make sure you paste "text only", and not something pre-formatted (as your post is).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    2
    Nevermind, guys. Figured it out. I was missing a semicolon after the first "if" statement. Oh, beginner programmer problems. Thanks everyone!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > I was missing a semicolon after the first "if" statement.
    Huh?

    You mean you changed
    if (change2 >= 25)
    into
    if (change2 >= 25);

    Nope - that just make the if statement redundant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Coin Counting Program
    By woodyhavoc in forum C Programming
    Replies: 8
    Last Post: 03-14-2011, 02:15 AM
  2. Help coin program.
    By Kinto in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2009, 08:00 PM
  3. Problems with a simple coin flipping program
    By Thileepan_Bala in forum C Programming
    Replies: 14
    Last Post: 01-18-2008, 07:33 PM
  4. Another Coin Counting Program
    By MipZhaP in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2005, 01:02 AM
  5. Coin counting (logic problem)
    By xlokix in forum C++ Programming
    Replies: 47
    Last Post: 01-20-2005, 02:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21