Need help using modular division in C++

My school assignment requires me to write a program to find out the change left after you input the amount tendered. my teacher tells us to use the % syntax which returns the remainder after division. i tried that but it gives me a error about using %with type double variables. but i must have it type double because i need to find the change in cents and double is the only variable that supports decimals. i have just started learning c++ so please help. thanks.
accesspointAsked:
Who is Participating?
 
nagravesCommented:
Well..

Typecast twice if you want that i suppose, you could do this:

    double priceInDollars = 11.27;  // 11.27 dollars
    int       priceInCents   = (int)(priceInDollars*100); // 1127 cents
    double amountTendered = 15.00;  // 15.00
    int       amountInCents =(int)(amountTendered*100);  1500

    int changeInCents = amountInCents - priceInCents; // 373
    int changeDollars = changeInCents/100; // 3
    int changeCents = changeInCents%100; // 73
    double finalChange = ((double)(changeDollars)) + ((double)(changeCents)/100); // 3.73
//
//  This is basically what is going on...
//   you input the price in dollars as a double amount ( 11.27 )
//   you convert that to cents by typecasting..  (int)(priceInDollars)
//   you input the amount of cash tendered in dollars as a double (15.00)
//    you convert that to cents by typecasting.. (int) (amountTendered)
//  
//    You find the amount of change in cents by simple subraction
//     You find the amount of dollars by dividing the changeincents by 100
//     you find the amount of change in by using %
//     you typecast back to a double, adding the the typecasted dollars to the typecasted change
//     which is divided by 100 to give you the decimal.

NOW I MUST WARN YOU... It is a bad idea to typecast back into a float, because you will be sure to get wrong answers! In my opinion you should just leave your output as int values and have it read  "The change is <changeDollars> dollars and <changeCents> cents."
0
 
nagravesCommented:
% or 'mod' is used with integer division.

int x = 9;
iny y = 5;
int z = 0;

z = x%y;

z is now equal to 4.  the modulus gives you the remainder.
0
 
nagravesCommented:
To add to my comment earlier, you could first multiply your double or float by 100 and then typecast to int. then use %.

None the less, % is an integer operator and cannot be used with floating point numbers.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
itsmeandnobodyelseCommented:
>>>> i must have it type double because i need to find the change in cents

No, the number of cents is an integer and not a double:

    double priceInDollars = 11.27;  // dollars
    int       priceInCents   = (int) (price*100);

    int dollar  = priceInCents/ 100;         // integer division gives the right number
    priceInCents = (priceInCents%100);  // calculate remainder
    int quarter    = priceInCents / 25;      // and so on ...
    ...

Regards, Alex


0
 
itsmeandnobodyelseCommented:
Correction:

   int       priceInCents   = (int) (priceInDollars*100);
0
 
nagravesCommented:
Thank you itsmeandnobodyelse. You have demonstrated what I wrote quite well. I didn't think to give an example of typecasting for the kid. :)
0
 
accesspointAuthor Commented:
ok so if im understanding correctly, i first have to typecast to int before i can use the mod division and then divide by 100 to get back the decimal?
0
 
itsmeandnobodyelseCommented:
>>>> then divide by 100 to get back the decimal?

No, the results you need - as nagraves told you - are integers. The numbers of dollars, quarters, nickels, cents, all are integers. Actually, you don't need any typecasting if you calculate the number of cents like that:

    double d = 12.27;
    int       c = 100 * d; // 1227

>>>> int changeDollars = changeInCents/100;

What you see here is an integer division, i. e. the result is an integer - not a decimal -  and that is good as we need the number of dollar bills here and not the decimal value of the change.

Regards, Alex

0
 
nagravesCommented:
Well, I'd like the points :)
0
 
accesspointAuthor Commented:
sorry guys that i took so long to respond. i didn't even realize i had a open question until i got the email!

anyways, i tried what nagraves did and it worked out fine. thanks guys.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.