I'M working my way through a C++ book and now I'M doing a card deck program. Here's the line I'M having trouble with.

r = card % 13;     // r = random 0 to 12

card in this case is just a random int number from 0 to 51.

52 diveded by thirten is 4. So if the % operator returns the remainder of a random number from 0 to 51 then as far as I can tell it's always going to be a decimal, is it not? Not a whole number from 0 to 12 as the comment would have me believe. Also, how can an int even be a deciaml? Sorry, I know there something really simple I'M missing here.

%, as you've stated is the remainder of a division operation. If you do long-hand division of two numbers, the value you have left at the end of the division is the remainder - this is always a whole number. Consider the following:

 7 | 12
      5 <- this is the remainder

Thus, 12 / 7 is 1 and 12 % 7 is 5 (the remainder).

a = q*b + r, where 0<=r<b,

then a/b=q and r is the remainder, for example
5 = 2 * 2 + 1
so 5/2=2 and 5%2=1

Edited 4 Years Ago by pyTony

This question has already been answered. Start a new discussion instead.