Go Fish game - program stops abruptly and won't enter loop

I'm writing a Go Fish command-line program in C++. I'm using Xcode. However, for some reason, the program won't enter the for loop at the end of the code. I'm thinking it has to do with the program not generating a number 0, but I've looked through the code several times and I am unable to exactly pinpoint the problem. Here is my code:

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
102
103
104
105
106
//
//  main.cpp
//  hugetest
//
//  Created by Måns Nilsson on 2013-09-22.
//  Copyright (c) 2013 Måns Nilsson. All rights reserved.
//

#include <iostream>
#include <string>

using namespace std;

int main(int argc, const char * argv[])
{
    srand(unsigned(time(0)));
    int player[4][30], tagen[52], sjon[46], random, antal, spelare_fraga, kort_fraga_real;
    string kort_fraga;
    
    cout<<"How many players? (2-4): ";
    cin >>antal;
    
    while (antal != 2 && antal != 3 && antal != 4) {
        cout<<"You must choose between 2-4 players! Try again: ";
        
        cin.clear();
        
        cin >>antal;
    }
    
    for (int k = 0, a = 0, b = 0;a<antal * 3;k++, a++) {
        if (k == antal) {
            k = 0;
            b++;
        }
        else { }
        
        random = (rand()%52);
        
        while (tagen[random] == 1) {
            random = (rand()%52);
        }
        
        player[k][b] = random;
        tagen[random] = 1;
    }
    
    
    
    
    cout<<"Player 1's cards: "<<endl;
    for (int k = 0;k<3;k++) {
        cout<<player[0][k]<<endl;
    }
    cout<<"Player 2's cards: "<<endl;
    for (int k = 0;k<3;k++) {
        cout<<player[1][k]<<endl;
    }
    
    cout<<"The sea cards: "<<endl;
    for (int a = 0;a<52 - (antal * 3);a++) {
        random = (rand()%52);
        
        while (tagen[random] == 1) {
            random = (rand()%52);
        }
        
        sjon[a] = random;
        tagen[random] = 1;
        cout<<sjon[a]<<endl;
    }
    
    for (int k = 0;k<4;k++) {
        if (k == antal) {
            k = 0;
        }
        else { }
        
        cout<<"Player "<<k + 1<<", who do you wish to ask? Choose a number between 1-"<<antal<<" but not yourself: ";
        cin >>spelare_fraga;
        
        cout<<"Which card would you like to ask player "<<spelare_fraga<<" about?: ";
        kort_fraga = "";
        cin >>kort_fraga;
        if (kort_fraga == "ESS" || kort_fraga == "Ess" || kort_fraga == "ess" || kort_fraga == "ÄSS" || kort_fraga == "Äss" || kort_fraga == "äss") {
            kort_fraga_real = 1;
        }
        else if (kort_fraga == "KNEKT" || kort_fraga == "Knekt" || kort_fraga == "knekt" || kort_fraga == "KNÄKT" || kort_fraga == "Knäkt" || kort_fraga == "knäkt") {
            kort_fraga_real = 11;
            
        }
        else if (kort_fraga == "DAM" || kort_fraga == "Dam" || kort_fraga == "dam") {
            kort_fraga_real = 12;
        }
        else if (kort_fraga == "KUNG" || kort_fraga == "Kung" || kort_fraga == "kung") {
            kort_fraga_real = 13;
        }
        else if (kort_fraga.size() == 2) {
                kort_fraga_real = ((kort_fraga[0] - '0') * 10) + (kort_fraga[1] - '0');
            }
        else {
                kort_fraga_real = kort_fraga[0] - '0';
            }
        
    }
}


I'm expecting the program to give player 1 three random cards between 0-51, and player 2 three other random cards between 0-51, and then the sea should get the remaining 46 cards between 0-51. The program stops abruptly after giving out the cards.
Last edited on
1
2
3
4
5
for (int k = 0;k<4;k++) {
        if (k == antal) {
            k = 0;
        }
        else { }


Blank else? Hmm...

Also, you can put in 3 or 4 players, but you only deal to two players. I would also suggest using an array to hold a standard 52 card deck for Go Fish, since it's a set matching game which really requires a standard deck with 4 of each number.
I just found out what the problem was. I had to declare all array values as 0 before beginning. I achieved this by adding for loops, and the program now works as expected. From now on, I will always declare the values for all array indexes before beginning.

My plan is to have the four A's (1's) as cards 1-4, the four 2's as cards 5-8, the four 3's as cards 9-12 and so on. Because we've generated 52 random cards, no cards can have the same value. If a card slot has the value 0, it will be treated as a non-card (or an empty slot). (I've also modified the code so the program now generates numbers between 1-52, not 0-51.)

By the way, are empty elses problematic? I don't think so, because it tells the program that, should none of the other conditions be met, nothing should happen.
If there is no else, nothing else will happen anyway.

Also note that rand() % 52 generates numbers 0-51 inclusive. You also might get an overflow on player[4][30]. Why not just make it [4][52] and never ever ever have the issue of that overflow? ;)
As I said, it clarifies that nothing should happen if none of the conditions in the preceding if statements are met. Regarding the overflow, I first considered it unlikely that a player would ever have over 30 cards in their hand at once, but I've changed it to [4][52] now.
Topic archived. No new replies allowed.