Question on creating a stardard deck class

Hi,

I'm currently coding five card poker game.
But as I was trying to set up the deck, some strange thing happened.

Here are my codes.


This is my main.cpp
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

#include <iostream>
#include <vector>
#include <algorithm>
#include <card.h>

using namespace std;

int main()
{
    vector<Card> deck;
    Card c;

    for(int i=0;i<52;i++){
        for(int j=2;j<15;j++){
            c.setRank(j);
            deck.push_back(c);
        }
    }

    for(int k=13;k<26;k++){
        deck[k].setSuit(2);
    }

    for(int l=26;l<38;l++){
        deck[l].setSuit(3);
    }

    for(int m=38;m<51;m++){
        deck[m].setSuit(4);
    }

    for(int x=0;x<deck.size();x++){
        deck[x].print();
    }


    cout << endl << endl;


    deck[15].print();

    return 0;
}



Card.h

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
#ifndef CARD_H
#define CARD_H

#include <string>

using namespace std;

class Card
{
    public:
        Card();
        Card(int a, int b);

        void setRank(int r);
        void setSuit(int r);

        int getRank();
        int getSuit();

        void print();

        string SuitToString();
        string RankToString();

        bool operator<(const Card &c) const {return myRank < c.myRank;}

    protected:
    private:
        int myRank;
        int mySuit;

};

#endif // CARD_H



Card.cpp
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
#include <iostream>
#include "Card.h"

Card::Card()
{
    //default case
    mySuit = 1;
    myRank = 1;
}

Card::Card(int a, int b)
{
    setRank(a);
    setSuit(b);
}

int Card::getRank()
{
    return myRank;
}

int Card::getSuit()
{
    return mySuit;
}

void Card::setRank(int r)
{
    myRank = r;
}

void Card::setSuit(int r)
{
    mySuit = r;
}

void Card::print()
{
    cout << RankToString() << " of " << SuitToString() << endl;
}

string Card::SuitToString()
{
    if(mySuit == 1)
        return "Spades";
    if(mySuit == 2)
        return "Hearts";
    if(mySuit == 3)
        return "Clubs";
    if(mySuit == 4)
        return "Diamonds";
}

string Card::RankToString()
{
    if(myRank == 2)
        return "Two";
    if(myRank == 3)
        return "Three";
    if(myRank == 4)
        return "Four";
    if(myRank == 5)
        return "Five";
    if(myRank == 6)
        return "Six";
    if(myRank == 7)
        return "Seven";
    if(myRank == 8)
        return "Eight";
    if(myRank == 9)
        return "Nine";
    if(myRank == 10)
        return "Ten";
    if(myRank == 11)
        return "Jack";
    if(myRank == 12)
        return "Queen";
    if(myRank == 13)
        return "King";
    if(myRank == 14)
        return "Ace";

}


I used:

1
2
3
4
5
6
7
for(int i=0;i<52;i++){
        for(int j=2;j<15;j++){
            c.setRank(j);
            deck.push_back(c);
        }
    }


to set the values of the cards and the code below to assign the suits.

But I'm quite confused because when I try to print them out as a whole
using this code:

1
2
3
 for(int x=0;x<deck.size();x++){
        deck[x].print();
    }


I get suit of all cards as spades.
But as you can see, I used deck[15].print(); to see if it really went wrong. It came out to be right when I use deck[15].print();.

I'm very puzzled about this...Help!

Thanks!

Your output:
Two of Spades
Three of Spades
Four of Spades
Five of Spades
Six of Spades
Seven of Spades
Eight of Spades
Nine of Spades
Ten of Spades
Jack of Spades
Queen of Spades
King of Spades
Ace of Spades
Two of Hearts
Three of Hearts
Four of Hearts
Five of Hearts
Six of Hearts
Seven of Hearts
Eight of Hearts
Nine of Hearts
Ten of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts

[649 lines skipped]

Ace of Spades
First 52 cards do have variable suits. Other 624 are spades.
Last edited on
@MiiNiPaa

Thanks for a reply.

I just figured out that I had a problem with the nested loop I used there.
Is there any way that I can exclude other 624 spades?
In your inner loop you are creating 13 cards — one suit. As there is 4 suits in deck your outer loop should run only 4 times.
@MiiNiPaa

Haha I felt so stupid after all

Thanks a bunch for the help!
Topic archived. No new replies allowed.