2

I have created card and deck classes for use in a C++ poker game, however, I'm trying to check that my assignment of vector elements are correct and having issues displaying them on the console. I have an enumerated class for the suits and integer values for the ranks. I see that my deck vector is generating with 52 cards, but I'm not sure how to correctly get this to display on the screen using the card class display() function for each vector element. Any help would be appreciated.

Note: I'm also guessing an alternate method would be to overload the output stream operator to display a vector element that is an object with an enumerated class parameter, but also not sure on what that syntax would look like.

int main(){ Deck deck();}

Card.h

#pragma once
#include <stdlib.h>
#include <iostream>
using namespace std;

enum class Suit { Hearts, Diamonds, Spades, Clubs };

class Card
{
private:
    Suit m_suit;
    int m_rank;
public:
    Card();
    Card(int r, Suit s);
    void display();

    int getRank();
    void setRank(int);

    Suit getSuit();
    void setSuit(Suit);

};

Card.cpp

#include "Card.h"
#include <iostream>
#include <string>


Card::Card(): m_rank(0), m_suit(Suit::Hearts)
{
    
}


Card::Card(int r, Suit s)
{
    m_rank = r;
    m_suit = s;
}

void Card::display()
{
    if (m_rank == 1)    // for number cards, show value
    {
        std::cout << "Ace";
    }
    else if ((2 <= m_rank) && (m_rank <= 10))
    {
        std::cout << m_rank;
    }
    else
    {
        switch (m_rank)
        {
        case 11:
            std::cout << "Jack";
            break;
        case 12:
            std::cout << "Queen";
            break;
        case 13:
            std::cout << "King";
            break;
        default:
            std::cout << "INVALID RANK";

        }
    }

    switch (m_suit)         // display suit
    {
    case Suit::Hearts:
        std::cout << " of Hearts";
        break;
    case Suit::Diamonds:
        std::cout << " of Diamonds";
        break;
    case Suit::Spades:
        std::cout << " of Spades";
        break;
    case Suit::Clubs:
        std::cout << " of Clubs";
        break;

    };
}

int Card::getRank()
// return the numeric value of a card
{
    return (m_rank);
}

Deck.h

#pragma once
#include <vector>
#include "Card.h"
class Deck
{
private:
    Card card;
    std::vector<Card> m_deck;
public:
    Deck();
    void shuffle();
    void grabCard();
};

Deck.cpp

#include "Deck.h"
#include "Card.h"
#include <vector>

Deck::Deck()
{
    
    for (int i = 1; i < 14; i++)
    {
        for (int j = int(Suit::Hearts); j <= int(Suit::Clubs); j++)
        {
            m_deck.push_back(Card(int(i), Suit(j))); //passing the card constructor to create a card object, then store it as a vector element
            int counter = 0; // use to print out each element
            std::cout << m_deck[card.display()]; //output each card obect via display method
            std::cout << "\t\n";
            counter++;
        }
    }

}
void Deck::shuffle()
{
    

}
void Deck::grabCard()
{

}
mrsazonn
  • 57
  • 4
  • Aside -- if you are actually playing poker, the suits are ranked, in descending order of precedence, Space, Hearts, Diamonds, Clubs... See also - [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) – David C. Rankin Jun 28 '20 at 08:23
  • @DavidC.Rankin good tip! I'll adjust them in the enumerations list as the proper value order assigned might come in handy for determining hands. As for using namespace std, I began removing it certain places and forget it card.h, but you are correct on it being bad practice! – mrsazonn Jun 28 '20 at 08:31
  • Yes, I saw that in Card.cpp. It's good to start with good habits -- it's much easier than breaking bad ones later `:)` – David C. Rankin Jun 28 '20 at 08:32

1 Answers1

3

To display vector element, you should call display() of the vector element to display.

std::cout << m_deck[card.display()];

should be

m_deck.back().display();

to have it display the last pushed card.

Also int main(){ Deck deck();} is a main function with only function declaration and Deck::Deck() won't be called.
To call the constructor, it should be int main(){ Deck deck;}.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70