# Thread: Casting to an Enum

1. ## Casting to an Enum

This section of code was taken out of a program that assigns an array of 52 card objects a number and a suit (a deck of cards). Then it displays the ordered deck, shuffles it and then displays the shuffled deck.

Heres the enum:
Code:
` enum Suit { clubs, diamonds, hearts, spades };`
Here's the excerpt of code that my question lies. My problem is highlighted in red:
Code:
```...
void main()
{
card deck[52];

cout << endl;
for(int j=0; j<52; j++)
{
int num = (j % 13) + 2;   // cycles through 2 to 14, 4 times
Suit su = Suit(j / 13);    // cycles through 0 to 3, 13 times
deck[j].init(num, su);  // sets card member data
}
}...```
I don't understand why (j / 13) is casted. Wouldn't
j / 13 be the same without the Suit cast?

2. >Wouldn't j / 13 be the same without the Suit cast?
Try removing it and see.

3. > void main()
Not your only mistake, main returns int

> Wouldn't j / 13 be the same without the Suit cast?
Whilst enums are represented using ints, an int isn't an enumeration.

4. dang i forgot to change my void main before posting, ok..don't comment on it again, it's over.

Salem, but you can assign and enum an integer value Ex.
Suit su = 0 is the same as
Suit su = clubs;

5. ....or anyone else could anser, even though i posted salem..

6. enum Suit { clubs = 1, diamonds = 2, hearts = 4, spades = 8 };

Which one still works?
Suit su = 0; // this one
Suit su = clubs; // or this one

The point is, you might "know" that the enumerations have the values 0 to 3 and your calculation returns values 0 to 3, but that doesn't make it an automatic thing as far as the compiler is concerned.

If you don't want to be hampered by type safety, then do
#define clubs 0
#define diamonds 1
But I wouldn't recommend that for C++ code