[2 days left] Whatâ€™s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# Need help using modular division in C++

Posted on 2004-10-25
Medium Priority
977 Views
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.
0
Question by:accesspoint
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3
• 2

LVL 4

Expert Comment

ID: 12406428
% 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

LVL 4

Expert Comment

ID: 12406458
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

LVL 39

Expert Comment

ID: 12408271
>>>> 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

LVL 39

Expert Comment

ID: 12408280
Correction:

int       priceInCents   = (int) (priceInDollars*100);
0

LVL 4

Expert Comment

ID: 12416205
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

Author Comment

ID: 12418390
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

LVL 4

Accepted Solution

nagraves earned 150 total points
ID: 12418602
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

LVL 39

Expert Comment

ID: 12418879
>>>> 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

LVL 4

Expert Comment

ID: 12661218
Well, I'd like the points :)
0

Author Comment

ID: 12684847
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generatâ€¦
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article locâ€¦
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor anâ€¦
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Botâ€¦
###### Suggested Courses
Course of the Month14 days, 18 hours left to enroll