Help creating a deck of cards please.

Hello, I'm new to C++ and I am currently learning it for my course. We have been set the task of creating and displaying a deck of cards. The brief for the assignment states that I must use a class.
A card will have the following operations: Constructor that takes two arguments; an integer and a string. Function to return the suit attribute. Function to return the value attribute.
"I expect the Ace, Jack, Queen and King values to displayed as strings but stored as integers."
As we are all new to C++, most of the class (college class, not c++ class) are pretty confused about how to go about it.

This is what I have so far. (C++ hasn't worked itself into my head that well yet) We are using Code Blocks to write our programmes.
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
#include <iostream>
#include <string>

using namespace std;

class Card
{
    private:
        string Suit;
        int Num;

    public:
        Card(int s, int n)
        {
           Suit = s;
           Num = n;
        }

};


#include <iostream>
#include "class.cpp"

using namespace std;

int main()
{
    int Deck[52], x, y;
    for (x=0;x<4;x++)
    {
        for (y=0;y<13;y++)
        {
            switch (y)
            {
                case 0: ; break;
                case 1: ; break;
                case 2: ; break;
                case 3: ; break;
                case 4: ; break;
                case 5: ; break;
                case 6: ; break;
                case 7: ; break;
                case 8: ; break;
                case 9: ; break;
                case 10: ; break;
                case 11: ; break;
                case 12: ; break;
            }
            switch (x)
            {
                case 0: ; break;
                case 1: ; break;
                case 2: ; break;
                case 3: ; break;
            }
        }
    }
    return 0;
}


Obviously there is a lot missing. I would be very grateful if somebody could give me some pointers without straight up what to do as I would like to work out mostly how to do it myself.

Sorry if what I've said isn't clear, and feel free to tell me what I've got so far is wrong :)
Last edited on
Card(int s, int n) should be Card(string s, int n), surely?

int Deck[52]
That creates an array of 52 int objects. I thought you wanted 52 Card object?
Ah yes, forgot to change the int into a string in the public, thank you.

I tried changing it to use Card instead but it didn't like it as I'm an idiot and I'm doing something wrong.

I put
1
2
int x, y;
Card Deck[52];

It says "error: no matching function for call to 'Card::Card()' "
Last edited on
I know someone will shoot me for it, but I like to use the following format for member functions with the express purpose of returning internal values:

1
2
3
4
5
6
7
8
9
10
class Card
{
  private:
    string _suit;
    int _num;
  public:
    Card(int n,string s) : _num(n), _suit(s) {}
    string Suit() { return _suit; }
    int Num() { return _num; }    
};


An easy way to coordinate strings with linear lists could be:

1
2
enum values { ONE=0,TWO,THREE,FOUR,... };
const char *card_names[] = { "One", "Two", ... , "King", "Ace" };


closed account (o1vk4iN6)
Someone else is doing something similar in the other subsection if it helps with anything:
http://cplusplus.com/forum/general/59066/
Someone else is doing something similar in the other subsection if it helps with anything:
http://cplusplus.com/forum/general/59066/


Ha thats my friend, didn't know he posted as well, amazing, well thanks for the help guys :)
The free ebook How to think like a Computer Scientist Learning with c++ may help.

It uses the example of simulating a deck of cards to illustrate features of c++.

Probably quickest to search thinkcscpp.
Last edited on
Danteeva wrote:
It says "error: no matching function for call to 'Card::Card()' "

Do you have a default constructor?
Topic archived. No new replies allowed.