Card Deck Problems

Hi, I am new to this site, and to C++. I have been trying to make a card deck and have no idea what to do or what I am doing wrong. Some of my code is missing because I didn't understand. I keep getting errors that say I need a pointer to object type, and I'm not sure how I should fix this.

/*

My name is **********

April 9, 2018

This program will play the cardgame War

*/

#include <iostream>
#include <ctime>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

class cardType {

private: string suit; int rank;

public:
cardType() {

rank = 0;
}

cardType(string s, int r) {

suit = s;
rank = r;
}
string getsuit(){
return suit;
}

int getrank() {

return rank;
}
};

int main() {

int j, n, c;
string card;
cardType c[52];

int i = 0;
for (n = 0; n < 4; n++) {
cardType cards(int i, int card);

for (j = 1; j < 14; j++) {

string suits;
switch (n) {
case 0: suits = "h";
break;
case 1: suits = "c";
break;
case 2: suits = "s";
break;
case 3: suits = "d";
break;
};
if (i < 13){
i++;
}
c[n] = cards;
n++;

}
}

for (c = 0; c < 52; c++) {
for (int z = 1; z < 14; z++) {
string rank;
switch (c) {
case 0: "Jack";
break;
case 1: "King";
break;
case 2: "Queen";
break;
case 3: "Ace";
break;

};

c[i] = cards(c, rank);
i++;
}
cout << c[i].getsuits() << "of" << c[i].getrank() << endl;
}
}
system("pause");

return 0;
}

Thank you for your time!
Do you have to use an array. Since you need to remove cards a vector would be much easier.
To initialize the cards you need 2 nested loops. One for all ranks and one for the suits.
I do not have to use an array, so I will try the vector. Thank you very much.
Actually, do you have any sample of coding I could look at for this?
Sometimes it's better to store the data differently from the way you display it. In this case, you should represent the suit as an enum. Or you might even represent a card just by it's value (0-51) and compute the suit and rank from the value: suit = value / 4, rank = value % 4.

Either way, you should create methods to convert a suit to a string, a rank to a string, and a card to a string (this would call the other two).

You might also create a separate Deck class that represents a deck of cards. The constructor would create a vector of 52 cards. A Deck might also have methods to shuffle the deck and deal a card.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*

My name is **********

April 9, 2018

This program will play the cardgame War

*/

#include <iostream>
#include <ctime>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

const char *suitName(unsigned suit)
{
    static const char *names[4] { "Hearts", "Clubs", "Spades", "Diamonds" };
    return names[suit];
}


class cardType
{
private:
    unsigned value;
public:
    cardType(unsigned v=0) : value(v) {}
    unsigned getsuit() const 
    {
	return value % 4;
    }

    unsigned getrank() const
    {
	return value / 4;
    }
    string name() const;
};

string cardType::name() const
{
    static const char *names[] = {
	"Ace", "1", "2", "3", "4", "5", "6",
	"7", "8", "9", "10", "Jack", "Queen", "King"
    };

    string result = names[getrank()];
    result += " of ";
    result += suitName(getsuit());
    return result;
}


class Deck {
private:
    vector<cardType> cards;
public:
    Deck();
    cardType deal();
    void shuffle() {
	random_shuffle(cards.begin(), cards.end());
    }
};

Deck::Deck() {
    for (unsigned i=0; i<51; ++i) {
	cards.push_back(cardType(i));
    }
}


cardType Deck::deal() {
    cardType result = cards.back();
    cards.pop_back();
    return result;
}



int
main()
{
    Deck deck;
    deck.shuffle();
    for (unsigned i = 0; i<52; ++i) {
	cout << deck.deal().name() << endl;
    }
}

Here's a very simple demo that might get you started.
I am not a card player so you need to check if I got the suits and ranks right.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

const int NUM_RANKS = 14;
const int NUM_SUITS = 4;

const string SUITS[NUM_SUITS] = { "clubs", "hearts", "spades", "diamonds" };
const string RANKS[NUM_RANKS] = { "", "Ace", "Two", "Three", "Four", "Five",
                         "Six", "Seven", "Eight", "Nine", "Ten",
                         "Jack", "Queen", "King" };


class Card
{
public:
  Card(string s, int r) 
  {
    suit = s;
    rank = r;
  }
  string getsuit() const
  {
    return suit;
  }

  int getrank() const
  {
    return rank;
  }
private: 
  string suit; 
  int rank;
};


class Deck
{
public:
  Deck()
  {
    
    for (int suit = 0; suit < NUM_SUITS; suit++)
      for (int rank = 1; rank < NUM_RANKS; rank++)
      {
        cards.push_back(Card(SUITS[suit], rank));
      }
  }
  void shuffle()
  {
    // TODO implement me
  }
  Card nextCard()
  {
  // TODO remove last card and return it  
  }
  void dump()
  {
    for (const Card& card : cards)
    {
      cout << card.getsuit() << "\t" << RANKS[card.getrank()] << "\n";
    }
  }
private:
  vector<Card> cards;
};

int main()
{
  Deck deck;
  deck.dump();
}
Thank you both, I will look at these!
Topic archived. No new replies allowed.