Connect Four C++ Seg Fault error

I am writing code for a simple connect four game, I have mostly everything working except when I fill out a column, once it gets to the top of the column it wont place the piece it will give me a Segmentation Fault (core dumped) error. I have a basic idea of why this is happening I just cant seem to find the problem.

Here is the code for the entire program, but I'm pretty sure the error is coming from the function "drop" at the bottom.

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cctype>

using namespace std;

struct game{
    char **board;
    char p1;
    char p2;
};

void print_board(game p, int r, int c);
bool play(game p, bool gamewon, int r, int c, int pieces);
bool check(int a, int b, game p, int pieces);
int drop(int b, char player, game p, int c);


int main(int argc, char *argv[]){
    game p;
    int r, c, pieces;
    bool gamewon = false;
    if(argc == 7) {
       for(int i = 1; i < argc; i += 2) {
          if(argv[i][0]=='-' && argv[i][1]=='r')
             r = atoi(argv[i+1]);
          else if(!strcmp(argv[i],"-c"))
             c = atoi(argv[i+1]);
          else if(!strcmp(argv[i],"-p")){
             pieces = atoi(argv[i+1]);
             while (pieces < 1){
                cout << "You cannot have 0 pieces to connect." << endl;
                cout << "Please enter a positive, non-zero integer"
                << " for the number of pieces to connect: ";
                cin >> pieces;
                }
          }
          else 
             cout << "Error." << endl;      
       }
   }
 
	for(int a =0; a < r; a++){		
		for(int b = 0; b < c; b++)	
			p.board[a][b] = ' ';		
	}								
	print_board(p, r, c);
	    gamewon = play(p, gamewon, r, c, pieces);
	return 0;
}



bool play(game p, bool gamewon, int r, int c, int pieces){
	int col, hold = 0, charsPlaced = 0;
	char player = 'y';
	while(!gamewon){
		if(hold != -1){
			if(player =='y'){
				cout<<"Player 1, what column do you want to put your piece? ";
				player = 'r';
			}
			else{
				cout<<"Player 2, what column do you want to put your piece? ";
				player = 'y';
			}
		}
		while(true){
			if(charsPlaced == r*c) break;
			cin>>col;
			col--;
			if(col <=r && col>= 0) break;
			else cout<< "\nPlease enter a value between 1 and " << c << ": ";
			if (cin.fail()){					
				cin.clear();		
				char d;			
				cin>>d;		
			}					

		}
		if(charsPlaced == r*c) break;
		hold = drop(col,player, p, c);
		if(hold == -1)	cout<<"Column is full!\nPlease enter another number between 1 and " << c << ": ";
		else{
			gamewon = check(hold,col,p, pieces);
			charsPlaced ++;
			print_board(p, r, c);
		}
	}
	if(charsPlaced == r*c){
		cout<<"No winner! Game is a draw\n";
		return true;
	}
	if(player == 'y')
		cout<<"Player 2 is the winner!\n";
	else cout<<"Player 1 is the winner!\n";
	return true;
	
}    

void print_board(game p, int r, int c){
    cout << endl;
    for(int a = 0; a < r; a++){
        for(int b = 0; b < c; b++) 
           cout << "|" << p.board[a][b];
           cout << "|";
           cout << endl;
           for(int i = 0; i < c; i++)
              cout << "--";
              cout << endl;
    }
}

bool check(int a, int b, game p, int pieces){
	int vertical = 1, horizontal = 1, diagonal1 = 1, diagonal2 = 1, i, j;
	char player = p.board[a][b];
	for(i = a + 1; p.board[i][b] == player && i <= 5; i++, vertical++);
	for(i = a - 1; p.board[i][b] == player && i >= 0; i--, vertical++);
	if(vertical >= pieces)
        return true;
	for(j = b - 1; p.board[a][j] == player && j >= 0; j--, horizontal++);
	for(j = b + 1; p.board[a][j] == player && j <= 6; j++, horizontal++);
	if(horizontal >= pieces) 
        return true;
	for(i = a - 1, j = b - 1; p.board[i][j] == player && i >= 0 && j >= 0; diagonal1 ++, i--, j--);
	for(i = a + 1, j = b + 1; p.board[i][j] == player && i <= 5 && j <= 6;diagonal1 ++, i++, j++);
	if(diagonal1 >= pieces) 
        return true;
	for(i = a - 1, j = b + 1;p.board[i][j] == player && i >= 0 && j <= 6; diagonal2 ++, i--, j++);
	for(i = a + 1, j = b - 1;p.board[i][j] == player && i <= 5 && j >= 0; diagonal2 ++, i++, j--);
	if(diagonal2 >= pieces) 
        return true;
	return false;
}

int drop(int b, char player, game p, int c){
	if(b >= 0 && b <= c){
		if(p.board[0][b] == ' '){
			int i;
			for(i = 0; p.board[i][b] == ' '; i++)
				if(i == 5){
                    p.board[i][b] = player;
			        return i;
            }
			i--;
			p.board[i][b] = player;
			return i;

		}
		else{
			return -1;
		}

	}
	else{
		return -1;
	}

}


Any help would be much appreciated, thanks!
How do you reproduce the segfault?
http://i.imgur.com/Lxp25TB.png

Once I try to add one more to column 1, it Seg faults.
Topic archived. No new replies allowed.