1

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++;
        }
    }
};
Jonah
  • 11
  • 1
  • 4
    I'd recommend using [`std::shuffle`](https://en.cppreference.com/w/cpp/algorithm/random_shuffle). – Fred Larson Nov 20 '19 at 22:34
  • 1
    Unrelated Recommended reading: [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – user4581301 Nov 20 '19 at 22:52

1 Answers1

0

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"
    ...
};
kaoao
  • 11
  • 4
  • I appreciate the response though I do not understand what it means to abstract the "Card" to a class. Does that mean create a class "Card" instead of having a string array? – Jonah Nov 25 '19 at 21:40