Thread: Help with a automated cash program! PLEASE!

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    Help with a automated cash program! PLEASE!

    Hey guys, this is my first post and I'm brand new to programming. My teacher is having me make an automated cash program that...

    1. Only allows the user to enter a dollar amount up to a 100, the input is a double and decimal places are used to represent cents. If the user types in more than two decimal places, use an arithmetic expression to remove the extra places.
    2. The 100 dollar limit must be enforced.
    3. Once the dollar amount is entered, your program will compute and display the amount entered, followed by a proper breakdown of denominations that represent that dollar amount, using the largest denominations possible. The following denominations are used: 20, 10, 5, 1, .25, .10, .05, and .01.
    4. If a denomination is not used, do not display that denomination at all.
    5. Have your results read singular or plurar as appropriate.
    6. After the breakdown has been output, ask the user if they want to enter another value to breakdown.

    I having so much issues with this program and I feel like an idiot because I bet it's something easy. I was hoping you guys can take a look at it and put me off on the right foot. I appreciate with any help you can offer me. Here's my code.
    in
    Code:
    /*Change-o-Matic*/
    
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        /* List of Variables*/
        double amtgiven;
        
        double twenty = 20.00;
        double ten = 10.00;
        double five = 5.00;
        double one = 1.00;
        double quarter = .25;
        double dime = .10;
        double nickle = .05;
        double penny = .01;
    
        printf("Please enter a dollar amount up to a hundered dollars: ");
            scanf("%lf", &amtgiven);
            
            /* To make sure the amount given stays under 100 dollars*/
            while ((amtgiven < 0) || (amtgiven > 100))
            {
                printf("The amount is out of range, please re-enter: ", amtgiven);
                    scanf("%lf", &amtgiven);
            }        
            
    
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the traditional approach is to do
    int pennyTotal = amtgiven * 100;

    Then use / and % to extract each denomination (all expressed as pennies) in turn.
    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.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Wow! I remember doing this problem when I learnt to code last century.

    This is all about division and the modulus operator '%'.

    Divide to get the amount (ie if input is $80, divide by the highest value (20) to find how many $20 you need).
    Then find the remainder (amtgiven % twenty = remainder)
    Divide the remainder by the next highest amount (ten) and find the remainder

    Rinse repeat.

    Print results.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can see you have your heart set on using doubles - but floats and doubles are a PITA to work with, simply because they will have to approximate values, and round off also.

    Take in amtGiven as a double (as required), and then immediately change it into pennies (which will be the basis of all your computations). This is slyly hinted at in the assignment's instructions, if you "read between the lines", a bit.

    Why do it?

    Because pennies are integers, and there is no more approximations, and no more rounding off of values. Aha!

    So pennies = 1, and nickels = 5, dimes = 10, etc., and they're all integers, instead of doubles. Everything becomes EZ-Peazy. All your arithmetic is almost trivial.

    When you're all done with your computation, you can do a bit of arithmetic to show dollars and cents, in the normal way.

    If you get stuck, post back, and tell us what has you stumped.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by alias View Post
    I having so much issues with this program and I feel like an idiot because I bet it's something easy. I was hoping you guys can take a look at it and put me off on the right foot. I appreciate with any help you can offer me. Here's my code.
    1) Take Adak's advice... work in pennies so you are not playing with decimal points at all. This is very simply done...
    Code:
    int amount = 0;
    
    do
      {
        float useramt = -1;
        do 
           { 
              printf("Amount to withdraw ($0.01 to $100.00) : "); 
              if ( scanf("%f", &useramt) < 1 ) 
                while ( getchar() != '\n');   // purge input on bad entries
            }
        while ( useramt < 0 );
        amount = (int) (amtgiven * 100.001);  // now it's pennies
       }
    while ( (amount < 0) || (amount > 10000));
    To display pennies as dollars use a simple trick...
    Code:
    printf("The amount is :  $ %d.%d",amount / 100, amount % 100);
    2) Stop trying to write code... start by trying to understand what you have to do. Become the cash register! How do you make change? What are the mathematic steps you need to figure out how many of the largest denomination? How much do you have left? What are the steps for the next denomination? and do on... Once you understand how to do this on paper you will find it's very easy to convert your hand written step by step procedure to code. This, for two reasons: First because you now understand the problem and second because you have it broken down in to small steps that are easily encoded.

    Programmers don't "write code"... they solve problems using computer languages... writing code is actually a very small part of what they do.
    Last edited by CommonTater; 09-20-2011 at 01:55 PM. Reason: updated input function to purge input buffer on bad entries

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    I kind of get what you guys are saying, thank you so much for the help! Another quick question, Tater, why are there two do's in that function? I've never seen that before. I have a feeling it's kind of a dumb question but does it make it do the do statement below it with the while and also the while statement that is attached to the first do?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    They're do-while loops. They're similar to your regular
    Code:
    while (x < 10) {
        // do some loopy stuff
    }
    but have one important difference: a do-while loop is guaranteed to execute at least once. Notice that the conditional part (the "while") part is at the bottom of the loop. The loop body is done first, then the condition checked, where a while and for loop checks the condition before executing the loop body (and thus may skip the loop body all together). This makes do-while loops great for user input, since you always want to ask for input once before checking if it's valid. If you haven't learned this yet, don't worry. There's a simple translation between the two:
    Code:
    do {
        some stuff
    } while (something is true);
    // is equivalent to
    some stuff
    while (something is true) {
        some stuff
    }

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    11
    So I think I'm sucking at life.. This is what I have so far...

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        /* List of Variables*/
        
                int amtgiven;
                
                int twenty = amtgiven * 20;
                int ten = amtgiven * 10;
                int five = amtgiven * 5;
                int one = amtgiven * 1;
                int quarter = amtgiven * 25;
                int dime = amtgiven * 10;
                int nickle = amtgiven * 50;
                int penny = amtgiven * 100;
            
            
            while ((amtgiven < 0) || (amtgiven > 100))
            {
                printf("The amount is out of range, please re-enter: ", amtgiven);
                    scanf("%i", &amtgiven);
            }        
            
    
            printf("The amount of all rates are: \n", twenty, ten, five, one, quarter, dime, nickle, penny); 
    
    
            return 0;
    }
    Last edited by alias; 09-20-2011 at 11:56 AM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by alias View Post
    So I think I'm sucking at life.. This is what I have so far...
    I highly doubt you're sucking at life in general, but you aren't exactly doing an awesome job of reading/listening/following instructions right now. Reread posts 2 and 3. They told you how to use division (/) and modulus (%) to get the answers you need.

    EDIT: I think I see the point of your confusion. What Salem was suggesting was that you take a quantity like $123.45, and turn it from dollars.cents to just cents, which is multiply by 100. I think the name pennyTotal might have thrown you off. Perhaps amount_in_cents would have been a better name. amount_in_cents = amount_in_dollars * 100;

    I also think you aren't following Tater's advice of "think before you act". Did you try to solve this by hand? If you did, you would recognize that multiply is not the right way to go. Think, if I ask for $10, your program would give me 200 twenties, 100 tens, 50 fives, 5 ones, 125 quarters, 50 dimes, 250 nickles and 1000 pennies. That's like going to the ATM, asking for $10 and getting $5283.75. Now, as long as my balance only went down $10, I'd love to use your ATM, but either way, it's wrong.

    Do this problem by hand:
    I want $436.93. How many twenties should you give me? Tens? Fives, ones, etc?

    The idea is to keep giving out twenties until the amount left to give out becomes less than twenty dollars. This is basically repeated subtraction, which in arithmetic, is just division. Do likewise for the tens, fives, etc. Get your hands on a game of Monopoly if need be, and work through the "dollar bills" portion of this assignment with "real" money. Once you figure that out, extending to include coins should be relatively easy.
    Last edited by anduril462; 09-20-2011 at 12:11 PM. Reason: Smart-alec-ness removed

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by alias View Post
    I kind of get what you guys are saying, thank you so much for the help! Another quick question, Tater, why are there two do's in that function? I've never seen that before. I have a feeling it's kind of a dumb question but does it make it do the do statement below it with the while and also the while statement that is attached to the first do?
    As Anduril has explained we use the do - while() construct to make sure everything gets done at least once...
    There are two "nested" loops there because you need to succeed on two fronts...
    First you need a valid input from scanf(). The user could enter FRED or something and in that case the scanf() has to repeat until it gets a numerical value...
    Once the inner loop is satisfied the entry is converted from dollars (in a double) to pennies (in an int).
    Then you need to check the range to make sure it's between 0 and $100 (10,000 pennies) to satisy the outer loop.
    If the outer loop is not satisfied you have to go right back to the beginning and ask the user to enter the amount again... until you get one in range.

    By the time both loops are satisfied, you *know* you have a number in a range the remainder of your code can work with...

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You are just confused because you are new to programming, so you've started by thinking about the outcome, creating all those different ints for different denominations, then you are not sure what to do with them.

    They are probably not necessary, if you think about this procedurally -- what should happen first/next -- and then keep proceeding from there. In short, don't create a variable until you have a concrete place in your code where it is needed.

    As Salem said in post #2, the key to this is modulus and division. Division on ints in C rounds down, and modulus (%) gives you the remainder:
    Code:
    	printf("Twenties: %d\n", pennies/2000);
    	pennies = pennies % 2000;
    	printf("Tens: %d\n", pennies/1000);
    	pennies = pennies % 1000;
    So here's an idea for you: use that divide - modulus scheme in a for(i++) loop with two parallel arrays like this:

    Code:
    char *labels[] = {"Hundreds", "Fifties", "Twenties" [... etc ...] };
    int divisors[] = {10000, 5000, 2000 [... etc ...] };
    Plug in labels[i] and divisors[i] appropriately in the loop.
    Last edited by MK27; 09-20-2011 at 12:32 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    I highly doubt you're sucking at life in general, but you aren't exactly doing an awesome job of reading/listening/following instructions right now. Reread posts 2 and 3. They told you how to use division (/) and modulus (%) to get the answers you need.

    EDIT: I think I see the point of your confusion. What Salem was suggesting was that you take a quantity like $123.45, and turn it from dollars.cents to just cents, which is multiply by 100. I think the name pennyTotal might have thrown you off. Perhaps amount_in_cents would have been a better name. amount_in_cents = amount_in_dollars * 100;

    I also think you aren't following Tater's advice of "think before you act". Did you try to solve this by hand? If you did, you would recognize that multiply is not the right way to go. Think, if I ask for $10, your program would give me 200 twenties, 100 tens, 50 fives, 5 ones, 125 quarters, 50 dimes, 250 nickles and 1000 pennies. That's like going to the ATM, asking for $10 and getting $5283.75. Now, as long as my balance only went down $10, I'd love to use your ATM, but either way, it's wrong.

    Do this problem by hand:
    I want $436.93. How many twenties should you give me? Tens? Fives, ones, etc?

    The idea is to keep giving out twenties until the amount left to give out becomes less than twenty dollars. This is basically repeated subtraction, which in arithmetic, is just division. Do likewise for the tens, fives, etc. Get your hands on a game of Monopoly if need be, and work through the "dollar bills" portion of this assignment with "real" money. Once you figure that out, extending to include coins should be relatively easy.
    @alias ... if I gave you 82.50 ...
    how many 20s would you give me ... why?
    how many 10s ... why?
    how many 5s ... why?
    how many 1s... why?

    and so on...

    Once you understand how making change actually works, you should be able to write the code...

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MK27 View Post
    You are just confused because you are new to programming, so you've started by thinking about the outcome, creating all those different ints for different denominations, then you are not sure what to do with them.

    They are probably not necessary, if you think about this procedurally -- what should happen first/next -- and then keep proceeding from there. In short, don't create a variable until you have a concrete place in your code where it is needed.

    As Salem said in post #2, the key to this is modulus and division. Division on ints in C rounds down, and modulus (%) gives you the remainder:
    Code:
    	printf("Twenties: %d\n", pennies/2000);
    	pennies = pennies % 2000;
    	printf("Tens: %d\n", pennies/1000);
    	pennies = pennies % 1000;
    So here's an idea for you: use that divide - modulus scheme in a for(i++) loop with two parallel arrays like this:

    Code:
    char *labels[] = {"Hundreds", "Fifties", "Twenties" [... etc ...] };
    int divisors[] = {10000, 5000, 2000 [... etc ...] };
    Plug in labels[i] and divisors[i] appropriately in the loop.
    Parallel arrays is probably a bit heavy duty for a new programmer MK... let him do it brute force this time.
    He can get fancy with it later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Automated CVS-VC6 building
    By andwan0 in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2009, 12:11 PM
  2. Help fixing my cash register program.
    By lil_rom in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2008, 02:28 PM
  3. Cash register program help :(
    By lil_rom in forum C Programming
    Replies: 2
    Last Post: 04-11-2008, 12:35 AM
  4. Security on automated home
    By stimpyzu in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-11-2004, 01:14 AM

Tags for this Thread