Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,218 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,157 people online right now. Registration is fast and FREE... Join Now!




Calcylating Change using % Issues

 
Reply to this topicStart new topic

Calcylating Change using % Issues, I searched but could not find the anwser

lockdown
1 Dec, 2007 - 02:51 PM
Post #1

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
So I spent about 30min searching the forums before putting this up. I found lots of topics on it but non that really explained methods used to get the change or code that worked.

The problem I have is calculating the change for quarter, dime, nickel and penny. Here is an example code I was using for dimes
CODE

int changedue = 0;
int amount = 15;
changedue = amount % .10 // Wont work due to the .10 I believe since its not a int data type correct?


Wont work due to the .10 I believe since its not a int data type correct?
So the issue is how would I get the change amount for quarter, dime, nickel and pennys?

This post has been edited by lockdown: 1 Dec, 2007 - 02:53 PM
User is offlineProfile CardPM
+Quote Post

Bench
RE: Calcylating Change Using % Issues
1 Dec, 2007 - 03:25 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Correct - and floating point operations don't have remainders after division, because floating point (or double precision floating point) values represent the fractional part of a number aswell.

Why not change your 'amount' to a number in pennies, and find the remainder in pence instead. (as opposed to a fraction of a pound or dollar or whatever)

eg,
CODE

    int amount = 1500;
    int changedue = amount % 10;


User is offlineProfile CardPM
+Quote Post

lockdown
RE: Calcylating Change Using % Issues
1 Dec, 2007 - 03:41 PM
Post #3

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
So converting to penny I am not sure how it would work. Here is the code im working with currently. At the end of my application I need to display each different amount so, it would show how many dollars, quarters, ect. How would changing them to pennys work?

CODE

        dollar = changeA % 1;
    changeA = changeA - dollar;

    quarter = changeA % .25;
    changeA = changeA - quarter;

    dime = changeA % .10;
    changeA = changeA - dime;

    nickel = changeA % .05;
    changeA = changeA - nickel;

    penny = changeA % .01;
    changeA = changeA - penny;

User is offlineProfile CardPM
+Quote Post

Bench
RE: Calcylating Change Using % Issues
1 Dec, 2007 - 05:17 PM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
This is the kind of operation where you really need to work it out with pen & paper to see what's going on. Use the windows calculator if it makes things easier.

Remember that modulus retrieves the remainder of a number, as if it had been divided by that number. eg, 10 mod 3 = 1
there's no such thing as remainder when dealing with floating point values (values which contain fractions of whole numbers)

One way to make sure that all your currency figures are whole numbers is to multiply them all by 100. so 25 dollars becomes 2500, and 0.01 dollars becomes 1. (you've changed the units from dollars to pennies. this ought to make your calculations alot simpler).


Here's a worked example in pseudocode. start with a value of $35.43

CODE

$35.43 dollars mult 100 == 3,543 pence

3543 pence div 100 == 35 dollars

3543 pence mod 100 == 43 pence

43 pence div 25 == 1 quarter

43 pence mod 25 == 18 pence

18 pence div 10 == 1 dime

18 pence mod 10 == 8 pence

8 pence div 5 == 1 nickel

8 pence mod 5 == 3 pence

Total:  $35.43
=========
35 Dollars + 1 quarter + 1 dime + 1 nickel + 3 pence.


This post has been edited by Bench: 1 Dec, 2007 - 05:25 PM
User is offlineProfile CardPM
+Quote Post

lockdown
RE: Calcylating Change Using % Issues
1 Dec, 2007 - 05:40 PM
Post #5

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
Thanks for explaining that it all make sense to me now. When I did it on paper like you suggested it made it a lot easier to see and think threw. I am still working on the remainder of my code for this program but here is the part I was having issues with.
CODE

    float changeA = 28.09;

    int dollar = 0;
    int quarter = 0;
    int dime = 0;
    int nickel = 0;
    int penny = 0;
    int changeC = 0;

    changeC = changeA * 100;


    dollar = changeC / 100;
    changeC = changeC % 100;

    quarter = changeC / 25;
    changeC = changeC % 25;

    dime  = changeC / 10;
    changeC = changeC % 10;

    nickel  = changeC / 5;
    changeC = changeC % 5;

    penny  = changeC / 1;
    changeC = changeC % 1;


I had to convert a floating number that would be coming into the function so like you suggested I multi. it by 100 along with all the change types. It is working great now thanks again.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 05:37AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month