#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    9
    Rep Power
    0

    Unhappy C++ Help! Separating Dollars and Cents


    OK guys, I'm in need oh felp from the C++ gurus again....

    i've written a program that takes a total cost of items a user wants to buy, and then I ask with how much money they are going to pay, i subtract the payment from the total cost and i get the change that i am supposed to give back. but here's the predicament.

    i am supposed to give back change using the least amount of bills and coins possible, and i just dont know how to do it. the highest bill denomiation is a $20, then 10, 5, and 1. Then I have to give back quarters, dimes, nickels, and pennies. For example, if the change is $36.41, i should output something like the following:

    Change = 1 - $20, 1 - $10, 1 - $5, 1 - $1, 1 - $.25, 1 - $.10, 1 - $.05, and 1 - $.01

    Any help ? Thanks!
  2. #2
  3. Google Relay Server
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Oh christ I don't even know any more.
    Posts
    1,812
    Rep Power
    438
    To use the least amount of bills and coins possible, you want to use as many large bills as possible. If you only were giving change in $5 and $1 bills, you would imagine that, to give back the least number of bills possible, first you would give back $5 bills until there was less than $5 in change remaining to give. Then you would give the rest in $1 bills.

    Same logic applies here but with more bills. Starting with the largest bill/coin, give back as many of those as possible. When you can't give back any more of those, go to the next largest bill/coin and try to give back the remaining change. Keep doing that until you have given all the change back; maybe some with pennies, so:

    - Keep track of how much change you have left to give back.
    - Give back as many $20's as possible, subtract $20*number of $20 bills you gave back from the original amount of change; this will be the amount of change remaining to give back. When there is less than $20 left to give back, move on to $10's. Then to $5's, $1's, quarters, dimes, nickles, then finally pennies.

    J.C.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    9
    Rep Power
    0
    so how do you code that?

    my professor have a few hints on how to go about doing this, but i dont understand.

    "Part 4:
    You should now have change in a variable of type double. One way to handle this next part is to separate the dollars from the cents and put them each in a variable of type int. Here is one way to separate them. If you type cast a double to an int, it drops the fractional part to make it an int. So, type casting your change to an int will automatically give you the dollars without the cents.

    The cents are a little trickier. You can get the double value of cents by subtracting the new value of dollars from the original value of change (change - dollars). Recall that this will result in a double value since change is a double. To make cents an integer, you will have to multiply by 100 and then type cast the result to an int.

    If all is well, you should now have two int variables, dollars and cents. For example, if your change was 50.34, dollars should equal the integer value 50, and cents should equal the integer value 34.

    Part 5:
    With these 2 integer values, you can now write the 2 functions. One function should take the int values for type of bill/coin and the remaining dollars/cents. This function should return the number of a that type of bill/coin to give back to the customer. You can use integer division for this. The second function should take the same 2 int values as the first function and return the remaining dollars/cents after that type of bill/coin has been given back. The % operator will give you the remainder.

    This remainder should be used in your next set of function calls for the next type of bill/coin."

    so how do you type cast a double in to an int?? :confused:
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    121
    Rep Power
    11
    To type cast into an int from double one would go, static_cast<insert type here>(variable);

    Replace "insert type here" with "int" in this case.

    Is this the second half of the same project you posted in the while thread? If it is mabye I will finish what I posted in there if you can not get this to work.
    Last edited by Ed----Ed; October 29th, 2003 at 09:08 PM.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    9
    Rep Power
    0
    yes it is the second half
  10. #6
  11. Google Relay Server
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Oct 2003
    Location
    Oh christ I don't even know any more.
    Posts
    1,812
    Rep Power
    438
    To cast from an int to a double, without using the relatively useless and less-readable static_cast operator, one would go:

    int n = 4;
    double f = (double)n;

    To go back, do:

    double f = 4.53;
    int n = (int)f;

    The variable n would take on the value of 4 there.

    A couple ideas that may or may not help you, but if you are still having trouble, feel free to ask about it some more:

    - The % (modulus) operator, used like A % B, computes the remainder when A is divided by B. You may or may not decide to use this in your solution.
    - If you convert your double price in dollars to an int number of cents (by multiplying by 100 and storing as an int), you may find it easier to work through the problem.
    - If A and B are ints, then A / B will return only the integer portion of A / B. So, (int)4 / (int)3 will be 1. If A and B are doubles, A / B will return a decimal number. Casting to an int will drop the decimal portion, since ints can only store integers: 4.0/3.0 is 1.3333 but (int)(4.0/3.0) is just 1.

    With that in mind, note that if I have $42.50, or 4250 cents, that (int)(4250/2000) is 2, meaning that $42.50 can be made of 2 whole $20 bills plus change. Those 2 $20 bills amount to 4000 cents, and 4250 original cents - 4000 cents in $20's gives 250 cents, or $2.50 left over. Consider:
    Code:
    int cents = 4250;
    int twenties = cents / 2000;
    cents = cents - 2000 * twenties;
    And, static_cast is only really useful when casting pointers to a class to pointers of a derived class. Even then, static_cast behaves exactly the same as the traditional casting operators; it does not do any special typechecking and is just as unsafe. Using dynamic_cast to cast things comes in handy in that it generates compile-time warnings to let you know if you screw up, and throws an run-time exception if you decide to ignore the compiler warnings. IMHO, "static_cast" only exists because whoever made that up felt that for some reason, "dynamic_cast" needed a similarly named opposite.

    Offense meant to static_cast<>, not to Ed----Ed. :P
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    121
    Rep Power
    11
    I don't like the way that static_cast looks, but it should be used instead of just (int)variable.

    static_cast is what is being promoted and I suspect that the other way may be eventually phased out.

    Even though this is a simple program that may never be recompiled in a future compiler, it is bad practise. It would not be a good idea to use the other method in code that may be edited later when that method is no longer supported.

    Its not really a big deal, I just don't want to tell people things that may no longer be supported.

IMN logo majestic logo threadwatch logo seochat tools logo