So what I am attempting to do is randomly populate a string array with a deck of cards. I am attempting to do this by "randomly" creating a number between 0-51 so that my array at that location will equal that enum suit followed by the enum value. So looking at my code you can see that the way I am setting the value in my array will cause an error. How do I go about accomplishing this while keeping the enumeration?
class DeckOfCards{
private:
string deck[52];
int sCount = 0;
int vCount = 0;
enum Suits{hearts, diamonds, clubs, spades};
enum Value{two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace};
public:
DeckOfCards(){
deck[52];
}
void shuffle(DeckOfCards noShuff){
srand(time(nullptr));
for(int i = 0; i < 52; i++){
deck[rand() % 52] = Suits[sCount] + Value[vCount];
sCount++;
vCount++;
}
}
};
Your first issue here is that you are attempting to reference the enum values of Suites
and Value
incorrectly. Value[0]
will not compile, but Value::two
will. Since this is not an enum class, 0
is also equivalent to Value::two
(just as 1
is equivalent to Value::three
).
Without solving the other issues in the code (like sCount
and vCount
not being bounded by the size of Suits
and Value
), you can try deck[rand() % 52] = static_cast<char>(sCount + vCount)
.
However, I do strongly recommend you abstract the "Card" to a class instead of using std::string
(see below).
class Card {
...
};
class DeckOfCards{
private:
Card deck[52]; <-- Deck of "Cards"
...
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.