Unknown Error

I'm experienceing an unknown error. My IDE (Visual Studio 2012) isn't giving me any errors. But when I run the program it crashes. More specifically after accepting the first input, it prints out an array. Either just before or right after the print out of this array the program crashes.

This error only occurred after I replaced everything in my code that was once a char to a newly created struct called "Slot." The easy answer would be to change it back and move on. But I believe this program will be easier in the end if I can have each array element hold two values. The first a bool value that tells me if the slot is filled or not, the second holding a char value.

I am open to suggestions, but I would rather keep my current format and make the needed adjustments. If I haven't already made it clear my compiler isn't pointing out any obvious syntax errors. It will compile, but it will also crash. I've tried setting breakpoints and stepping through the program, but it doesn't fail then. It always runs all the way through.

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
#include <iostream>
#include <iomanip>

const int EXTRA_ROWS = 3;
const int EXTRA_COLUMS = 2;

struct Slot{
	char type;
	bool filled;
};

int GetConnectionSize(void);
Slot** BuildGameBoard(int WinSize);
void DisplayGameBoard(Slot**GameBoard, int WinSize);


int main()
{
	int WinConnectionSize;
	Slot **Gameboard;
	WinConnectionSize = GetConnectionSize();
	Gameboard =BuildGameBoard(WinConnectionSize);
	DisplayGameBoard(Gameboard, WinConnectionSize);

	
    return 0 ;
}

int GetConnectionSize(){
	int WinSize;
	bool validSize = true;

	do{
		std::cout << "Enter the amount of connections requied to win the game: ";
		std::cin >> WinSize;
	
		if (WinSize < 3 || WinSize > 6){
			std::cout << "Invalid connect size!";
			std::cout << " Please enter a value between 3 and 6." << std::endl;
			validSize = false;
			std::cin.ignore(); //May have to remove
		}
		else{
			validSize = true;
		} 

	}while(validSize == false);

	return WinSize;
}

Slot** BuildGameBoard(int WinSize){
	Slot** GameBoard = new Slot *[WinSize+EXTRA_ROWS];
	
	for(int i = 0; i < (WinSize + EXTRA_ROWS); i++)
		{
			GameBoard[i] = new Slot[EXTRA_COLUMS];
		}
	// Create an empty gameboard
	for(int j = 0; j < (WinSize + EXTRA_ROWS); j++)
	{
		for(int k = 0; k < (WinSize+EXTRA_COLUMS); k++)
		{
			GameBoard[j][k].filled = false;
			GameBoard[j][k].type = ' ';
		}
	}
	return GameBoard;
}

void DisplayGameBoard(Slot**GameBoard, int WinSize){
	//print out the correct size top
	for (int w = 0; w < WinSize+EXTRA_ROWS; w++){
		if (WinSize+EXTRA_ROWS == 6 && w == 0){
			std::cout << " ===";
		}
		else if (WinSize+EXTRA_ROWS == 7 && w == 0){
			std::cout << " ====";
		}
		else if(WinSize+EXTRA_ROWS == 8 && w == 0){
			std::cout << " =====";
		}
		else if(WinSize+EXTRA_ROWS == 9 && w == 0){
			std::cout << " ======";
		}
		else{
		std::cout << "====";
		}
	}
	std::cout << std::endl;
	//start new line
	for (int i = 0; i < WinSize+EXTRA_ROWS; i++){
		for (int r = 0; r < WinSize+EXTRA_COLUMS; r++){
			std::cout << "| " << GameBoard[i][r].type << " |";
		}
		std::cout << std::endl;
	}
	for (int w = 0; w < WinSize+EXTRA_ROWS; w++){
		if (WinSize+EXTRA_ROWS == 6 && w == 0){
			std::cout << " ===";
		}
		else if (WinSize+EXTRA_ROWS == 7 && w == 0){
			std::cout << " ====";
		}
		else if(WinSize+EXTRA_ROWS == 8 && w == 0){
			std::cout << " =====";
		}
		else if(WinSize+EXTRA_ROWS == 9 && w == 0){
			std::cout << " ======";
		}
		else{
		std::cout << "====";
		}
	}
	std::cout << std::endl;
}


Thanks for taking a look.
Last edited on
line57 GameBoard[i] = new Slot[EXTRA_COLUMS]; shouldn't that be WinSize+EXTRA_COLUMNS otherwise when you loop you are going out of bounds. (btw it is columns ;P not colums
Solved! Thanks. Also made the spelling correction.
Topic archived. No new replies allowed.