Simple card dealing program.

Hi, I have two questions regarding this simple card dealing program:

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int rand_0toN1(int n);
void draw_a_card();
int select_next_available(int n);

bool card_drawn[52];
int cards_remaining = 52;

char *suits[4] =
    {"hearts", "diamonds", "spades", "clubs"};
char *ranks[13] =
    {"Ace", "two", "three", "four", "five",
     "six", "seven", "eight", "nine",
     "ten", "jack", "queen", "king"};

int main()
{
    int n, i;

    srand(time(NULL));

    while (1)
    {
        cout << "enter number of cards to draw (0 to exit): ";
        cin >> n;
        if (n == 0)
            break;
        for (i = 1; i <= n; i++)
            draw_a_card();
    }

    return 0;
}

// Draw-a-card function
// Perform a card draw by getting a random 0-4 and a
//   random 0-12. Use these to index the strings
//  arrays, ranks and suits.
//
void draw_a_card()
{
    int r;      // Random index (o thru 12) into ranks array
    int s;      // Random index (o thru 3) into suits array
    int n, card;

    n = rand_0toN1(cards_remaining--);
    card = select_next_available(n);
    r = card % 13;  // r = random 0 to 12
    s = card / 13;  // s = random 0 to 3
    cout << ranks[r] << " of " << suits[s] << endl;
}

// Select-next-available-card function.
// Find the Nth element of card_drawn, skipping over
//  those elements already set to true. 
//
int select_next_available(int n)
{
    int i = 0;

    while (card_drawn[i])
           i++;

    //while (n-- > 0)
    //{
    //    i++;
    //    while (card_drawn[i])
    //        i++;
    //}
    card_drawn[i] = true;
    return i;
}

int rand_0toN1(int n)
{
    return rand() % n;
}


My first question is why does this program crash if I input 52?

My second question is about the bold commented out section in the middle of the select_next_available() function. I copied this from a book and that section is not commented out in the book.

I was looking at the program, at that function and could not for the life of me figure out why that part was there and what it was doing so I commented it out and ran the program again. It works exactly like it should, there seems to be no difference. So my question is what does that commented out section actually do? Because it seems to do nothing.

Thanks
It crashes because you have an array of 52 cards. They go from 0 to 51 (not from 1 to 52), so if you try to get the 52nd member of that array, it will crash.
Strange, if I input 0 the program deals no cards, if I input 1, it deals 1 card. If I can't input 52 how do I deal all 52 cards?
You can't do it the way you're doing it now. The reason why it deals no cards when the input is 0 is because you explicitly told it so(lines 32-33):

1
2
if (n == 0)
            break;


I would change line 30-35 to:

1
2
3
4
5
6
cout << "enter number of cards to draw (-1 to exit): ";
        cin >> n;
        if (n < 0)
            break;
        for (i = 0; i < n; i++)
            draw_a_card();


Or something like that.
THanks I understand, but the commented out section of select_next_available() is still puzzling me. As far as I can tell it doesn't do anything which makes me wonder why the author of the book I copied the program from wrote it.
Could someone please comment on the commented out section of this function. Does it really do nothing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int select_next_available(int n)
{
    int i = 0;

    while (card_drawn[i])
           i++;

    //while (n-- > 0)
    //{
    //    i++;
    //    while (card_drawn[i])
    //        i++;
    //}
    card_drawn[i] = true;
    return i;
}

int rand_0toN1(int n)
{
    return rand() % n;
}
Without having read through it thoroughly, I agree with you that it seems superfluous. I don't really understand what it's meant to be doing either. If it works just as fine without it, then just leave it commented out, so that you still have it in case it starts to act weird;) Pretty weird to print code as errounous as this in a book :p
Last edited on
Topic archived. No new replies allowed.