drawing cards from a deck

Hi again :p

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
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <time.h>

using namespace std;

class Cards
{
    public:
    int ncardValue; // assigned 2-10. Assigned 0 for JQKA.
    string scardValue; // assigned to JQKA. Not defined for 2-10.
    string scardSuit; // Diamond, Club, Heart, Spade
    bool bcardDrawn; // false if still in deck.
};

Cards* buildDeck(void)
{
    Cards *cardDeck = NULL;
    cardDeck = new Cards[52];

    for (int i=0; i<52; i++)
    {
        cardDeck[i].bcardDrawn = false;

        cardDeck[i].ncardValue = 0;

        if ( i < 9 )
        cardDeck[i].ncardValue =         // all of these are equal. Offsets for each suit.
        cardDeck[i+13].ncardValue =
        cardDeck[i+26].ncardValue =
        cardDeck[i+39].ncardValue = i+2;

    if  (i > 0 && i <= 12)
        cardDeck[i].scardSuit = "Diamond";
    if  (i > 12 && i <= 25)
        cardDeck[i].scardSuit = "Club";
    if  (i > 25 && i <= 38)
        cardDeck[i].scardSuit = "Heart";
    if  (i > 38 && i <= 51)
        cardDeck[i].scardSuit = "Spade";

    if  ( (i+1)%10 == 0 ) // jack=9i (since 2=0i, 10 = 8i)
        cardDeck[i].scardValue = "Jack";
    if  ( (i+1)%11 == 0 )
        cardDeck[i].scardValue = "Queen";
    if  ( (i+1)%12 == 0 )
        cardDeck[i].scardValue = "King";
    if  ( (i+1)%13 == 0 )
        cardDeck[i].scardValue = "Ace";
    }
    return cardDeck;

}

Cards* drawCards(int numCards, Cards fullDeck[])
{
    Cards *userHand = NULL;
    userHand = new Cards[numCards];
    int currCard;

    for (int i=0; i<6; i++)
    {
        currCard = rand()%52;

        if (fullDeck[currCard].bcardDrawn != true)
        userHand[i] = fullDeck[currCard];

        fullDeck[currCard].bcardDrawn = true;
    }

    return userHand;

}


int main()
{
    Cards* deck = buildDeck();
    srand(time(0));
    int numCards;
    cout << "How many cards do you want to draw?" << endl;
    cin >> numCards;
    Cards* hand = drawCards(numCards, deck);

    /*for (int i=0; i<numCards+1; i++)
    {
        cout << i+1 << ": ";

        if ( hand[i].ncardValue == 0 )
            cout << hand[i].scardValue;
        if ( hand[i].ncardValue )
            cout << hand[i].ncardValue;

        cout << " " << hand[i].scardSuit << endl;
    }*/

    delete[] hand;
    delete[] deck;
    return 0;
}


Compiling it works. However, if I remove the /* and */ to add it to the code, it'll still compile, but the program will stop functioning. Windows gives me an error msg that cardDraw.exe has stopped working. Not sure what's going on here..
/* through */ is comment, which I guess you already know since you went through this code. Im not quite sure what you're asking though? The program doesn't work with it or without?
Last edited on
It doesn't work with it, but it still compiles.

What it's supposed to do is output the value and suit 5 Cards objects in the array hand[]. If the object is a J, Q, K, or Ace it should output that instead of a number.

What happens is the program freezes, and Windows gives me an error (not the compiler).
You're trying to iterate through more cards than there actually are (numCards+1), so you shouldn't be surprised.
By the way, it would be better if you create a Deck class that internally uses a vector to store the cards.
Oh, wow. I feel like an idiot.

I haven't learned about vectors yet; I'm about halfway through a beginner book, just trying to write some programs to practice what I've picked up.
vectors are a basic concept, so if they didn't appear in your book yet, you probably should be worried.
If they appear at all in the book, you might want to read that chapter first. vectors are basically dynamic arrays and are easy to use.
Thanks, I'll try looking up vectors. I can only find vectors in my book as a mention when it's talking about templates; not even close to that yet. :/

Looks like there were a ton of things wrong with the code, so going to have fun figuring out the problems now :P
Topic archived. No new replies allowed.