Change Maker (problems with %)

This is a discussion on Change Maker (problems with %) within the C++ Programming forums, part of the General Programming Boards category; OK, so for school, I have to make a program that illustrates the change a person should get based on ...

  1. #1
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76

    Question Change Maker (problems with %)

    OK, so for school, I have to make a program that illustrates the change a person should get based on a few monetary inputs. I'm crafting one that does so in the following steps:

    1. User inputs monetary value (e.g. 17.34 for $17.34) describing the original bill.

    2. User inputs monetary value describing how much cash they paid.

    3a. If the amount paid is less than the bill, it tells you that you still owe the difference (which is produced).

    3b. If the amount paid is more than the bill, it illustrates the most efficient change you should get, in dollars, quarters, dimes, nickels, and pennies.

    3b is where I'm stuck. I know that inputting, for example, (25 % 3) would return the remainder of 25 / 3, which is 1. However, I want to divide, say, 25 by 3, return the 8, and keep the one to work with in the next step.

    This is confusing me. D:

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    To get the eight, you would simply use integer math, ie 25 / 3 produces 8. 25 % 3 produces the one. Simply save them or whatever and continue processing.
    If you cannot grasp how to do that, then you need to write down the logical steps needed to perform that. Imagine someone owing you something, for example, and think logically how such transactions would be done.
    Btw, don't use goto.
    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.

  3. #3
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Quote Originally Posted by Elysia View Post
    Btw, don't use goto.
    I SAID NOT TO TERRORIZE ME! :'(

    So using the variables as integers in division just rounds them to the nearest whole number?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    More precisely, it truncates the decimals. Eg 11 / 6 = 1.
    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.

  5. #5
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    Oh, so it always rounds down?
    OK I'll give it a shot.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    No, it discards the decimals. Chops them off.
    Not quite the same thing, but I suppose you could say that.
    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.

  7. #7
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    I see what you mean.

    So I say:
    Code:
    cout << setprecision(0) << fixed << ((paid - bill) % 1);
    ?
    I assume that setprecision(0) would chop off the decimals as well. Kind of easier to manage how many decimal places for each statement individually, right?

    Would I use % or / 1?

    The for the next one, I'm not sure how to call in the leftovers from the last function.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    There is no need to use % 1 or / 1 at all. They will not do anything.
    The for the next one, I'm not sure how to call in the leftovers from the last function.
    You will need to formulate proper logic for it first. Eg, how would you do it, in real life?
    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.

  9. #9
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    I'm not trying to be difficult, but I'm really new to programming so I probably don't know half the objects or statements you're trying to get me to think of. I see where you're coming from, not spoon-feeding everyone answers and all, and I respect that, but I could use more straightforward answers to learn much better.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Not getting the objects or statements, or being really new isn't much of an excuse though. People who say things like that eventually get used to giving up at the slightest hardship and not doing anything. Not to mention this problem is entirely math based: if you paid $20 for a 17.34 bill then you could get $2, 2 quarters, 1 dime, 1 nickel, and 1 penny back. What did I really divide to get that, and why would you want to go on to a more difficult problem than this one, without figuring it out?


    You ask how you want to save the result of 25 / 3 or 25 % 3. What are variables for?
    Last edited by whiteflags; 03-08-2011 at 09:33 PM.

  11. #11
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by muffinman8641 View Post
    I'm not trying to be difficult, but I'm really new to programming so I probably don't know half the objects or statements you're trying to get me to think of. I see where you're coming from, not spoon-feeding everyone answers and all, and I respect that, but I could use more straightforward answers to learn much better.
    If you really don't understand what they are telling you, try to understand that.... You can't live with the knowledge you gain in your class rooms.... Try to get your own.... Internet is the best source of getting knowledge......

    Good luck..

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    % 1 gives the remainder of dividing something by one, which of course is always zero.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Quote Originally Posted by muffinman8641 View Post
    I'm not trying to be difficult, but I'm really new to programming so I probably don't know half the objects or statements you're trying to get me to think of. I see where you're coming from, not spoon-feeding everyone answers and all, and I respect that, but I could use more straightforward answers to learn much better.
    All I'm saying is this:
    Before getting into statements and objects and whatnot, you should think of how the program should behave. Ie, how do you WANT it to behave? The logical part.
    Then we can get into translating the logical part into statements and objects.
    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.

  14. #14
    Registered User muffinman8641's Avatar
    Join Date
    Feb 2011
    Location
    Eastern-Central PA
    Posts
    76
    OK, sorry if I came off as an ass there.

    I need to take the difference between the paid amount and the bill, then break it down into dollars and coins.

    I need to say "How many whole dollars fit into this?"
    Then subtract the dollars and say "How many quarters are in the remains?"
    etc.

    [Added note- I tried the suggestion of
    Code:
    int x, y;
    double a, b;
    for splitting up variable types, and it seemed to work ok. I'm just stuck on how to get a whole number with the decimal chopped off, because bill and paid are both originally double variables, since there's change involved with both. I tried making a new int variable called intdollars = (paid - bill), but I got a warning about that and it returned -2 billion dollars when I tested it.]
    Last edited by muffinman8641; 03-09-2011 at 07:56 AM.

  15. #15
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    The reason will be that paid and bill will be double datatype variables and double datatype occupies 8 bytes in the memory (C++), while integer just occupies 4 bytes (C++), so it truncates or process something differently while performing that operation...

    You must declare dollars variable as double datatype and then you can format the decimal point using flags or setprecision....
    Or
    Code:
    double num = 23.345; 
     int intpart = (int)num;

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Change maker program remainder troubles.
    By muffinman8641 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2011, 02:41 PM
  2. Making a change counting program
    By Scrogdor in forum C Programming
    Replies: 15
    Last Post: 02-23-2011, 01:00 PM
  3. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  4. Replies: 2
    Last Post: 09-04-2001, 03:12 PM

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