Question about enums

Hello. I have a question about enum and was looking for some help.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

enum cardSuit {SPADE, CLUB, HEART, DIAMOND};

void setSuit( cardSuit )

int main()
{
....
setSuit(0)
.....
}

setSuit( cardSuit suitin )
{
	if( 0 == suitin )
		suit = static_cast<char>( SPADE );
	else if( 1 == suitin )
		suit = CLUB;
	else if( 2 == suitin )
		suit = HEART;
	else if( 3 == suitin )
		suit = DIAMOND;
}


What I need to do is represent a SPADE, HEART, DIAMOND, CLUB as a character, but every time I try to cast the enum to a char I get an error. How can I make this work?

Thanks.
closed account (3hM2Nwbp)
Is this what you're trying to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
enum cardSuit {SPADE, CLUB, HEART, DIAMOND};

void setSuit( char suitIndex )

int main()
{
....
setSuit(0)
.....
}

setSuit( char suitIndex )
{
        // Mindful that suit is undefined here.
	if( 0 == suitIndex )
		suit = SPADE;
	else if( 1 == suitIndex )
		suit = CLUB;
	else if( 2 == suitIndex )
		suit = HEART;
	else if( 3 == suitIndex )
		suit = DIAMOND;
}
Last edited on
I need to know how to represent the enum as a character. Luc, can you explain how your code does that? I am new so bear with me.
Thanks
What do you mean by represent it as a character? A character is a number, by default ASCII. An enum is also just defining a number of numerical constants, for example
 
enum cardSuit {SPADE, CLUB, HEART, DIAMOND};


would be roughly equivalent to
 
const int SPADE = 0, CLUB = 1, HEART = 2, DIAMOND = 3;


Lucs code... doesn't really do anything. It defines a random char function that assigns a numerical value to a nonexistent variable, so I don't really get what he was trying to tell you.

Anyways: You can cast an enum to char. The thing is, casting spade to a character would result in \0 (ASCII 0), so you can't do it like that. Again, what exactly are you trying to achieve.
Last edited on
I need to fill a one character array with the suit, which is the enum cardSuit, followed by a character representing the value of the card and then add a null to the end.
I have got the value figured out and the null but I am struggling with the enum portion.

1
2
3
4
5
6
7
8
9
10
11
12
setSuit( cardSuit suitin )  // if cardSuit suitin is 0 then I want suit to equal 'S'
{
	if( 0 == suitin )
		suit = static_cast<char>( 'S' );
	else if( 1 == suitin )
		suit = CLUB;
	else if( 2 == suitin )
		suit = HEART;
	else if( 3 == suitin )
		suit = DIAMOND;
}


Thanks for the help
Topic archived. No new replies allowed.