Best way to handle big array declarations/output

Hey, I've been a fan of this site for a while, and up until this point I've been able to find answers to all my questions, but I'm afraid my problem at this point is lack of experience. What I'm trying to do is make a little Battleship-esque program which randomly chooses one square in each column of a 8x8 grid to be the "ship square." In each "turn" the user will pick a square and if it is a ship square, the ship will be sunk and the user tries again. Coding that part of it went pretty smoothly, but I want the program to show the updated grid after each turn, first with the coordinate designations (D3, F1, H8 etc), and then updated to "M" for "miss" if the user attacked the square (and there wasn't a ship on it) or a "X" if it was attacked and a ship was sunk.

The way that I tried to do this will probably seem extremely crude and inefficient, but it was the only way I could think of with my limited experience. Here's the code of the little side program I made to test storing and retrieving the coordinate designations in a string array:

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

using namespace std;

int yCoord;
int xCoord;
string boardResults[7][7];

int main()
{
    //Column A Declarations
    boardResults[0][0] = "A1";
    boardResults[0][1] = "A2";
    boardResults[0][2] = "A3";
    boardResults[0][3] = "A4";
    boardResults[0][4] = "A5";
    boardResults[0][5] = "A6";
    boardResults[0][6] = "A7";
    boardResults[0][7] = "A8";

    //Column B Declarations
    boardResults[1][0] = "B1";
    boardResults[1][1] = "B2";
    boardResults[1][2] = "B3";
    boardResults[1][3] = "B4";
    boardResults[1][4] = "B5";
    boardResults[1][5] = "B6";
    boardResults[1][6] = "B7";
    boardResults[1][7] = "B8";

    //Column C Declarations
    boardResults[2][0] = "C1";
    boardResults[2][1] = "C2";
    boardResults[2][2] = "C3";
    boardResults[2][3] = "C4";
    boardResults[2][4] = "C5";
    boardResults[2][5] = "C6";
    boardResults[2][6] = "C7";
    boardResults[2][7] = "C8";

    //Column D Declarations
    boardResults[3][0] = "D1";
    boardResults[3][1] = "D2";
    boardResults[3][2] = "D3";
    boardResults[3][3] = "D4";
    boardResults[3][4] = "D5";
    boardResults[3][5] = "D6";
    boardResults[3][6] = "D7";
    boardResults[3][7] = "D8";

    //Column E Declarations
    boardResults[4][0] = "E1";
    boardResults[4][1] = "E2";
    boardResults[4][2] = "E3";
    boardResults[4][3] = "E4";
    boardResults[4][4] = "E5";
    boardResults[4][5] = "E6";
    boardResults[4][6] = "E7";
    boardResults[4][7] = "E8";

    //Column F Declarations
    boardResults[5][0] = "F1";
    boardResults[5][1] = "F2";
    boardResults[5][2] = "F3";
    boardResults[5][3] = "F4";
    boardResults[5][4] = "F5";
    boardResults[5][5] = "F6";
    boardResults[5][6] = "F7";
    boardResults[5][7] = "F8";

    //Column G Declarations
    boardResults[6][0] = "G1";
    boardResults[6][1] = "G2";
    boardResults[6][2] = "G3";
    boardResults[6][3] = "G4";
    boardResults[6][4] = "G5";
    boardResults[6][5] = "G6";
    boardResults[6][6] = "G7";
    boardResults[6][7] = "G8";

    //Column H Declarations
    boardResults[7][0] = "H1";
    boardResults[7][1] = "H2";
    boardResults[7][2] = "H3";
    boardResults[7][3] = "H4";
    boardResults[7][4] = "H5";
    boardResults[7][5] = "H6";
    boardResults[7][6] = "H7";
    boardResults[7][7] = "H8";


    cout << "Enter the column coordinate of the square you would like to access: ";
    cin >> yCoord;

    cout << "Enter the row coordinate of the square you would like to access: ";
    cin >> xCoord;

    cout << "Here it is: " << boardResults[yCoord][xCoord];
    system("PAUSE");

    return 0;
}


Even I can tell it's terrible programming style/form/technique, but I was going to use it if it worked, and it should, but with the code you see above, the program crashes as soon as it launches. It does work perfectly if I delete the last line of the column G declarations and all of the column H declarations, though, so that might point towards some kind of overflow issue.

Now that I've explained what I'm trying to accomplish, is there a better way to declare all of the coordinate designations of the squares with an array so that it can be output to the user, and updated? If I did somehow make the code above work, the next problem that arises is that to output the grid after each turn, I end up with something like this...

1
2
3
cout << boardResults[0][7] << "|" << boardResults[1][7] << "|" << boardResults[2][7] << "|" << boardResults[3][7] << "|" << boardResults[4][7] << "|" << boardResults[5][7] << "|" << boardResults[6][7] << "|" << boardResults[7][7];
cout << boardResults[0][6] << "|" << boardResults[1][6] << "|" << boardResults[2][6] << "|" << boardResults[3][6] << "|" << boardResults[4][6] << "|" << boardResults[5][6] << "|" << boardResults[6][6] << "|" << boardResults[7][6];
cout << boardResults[0][5] << "|" << boardResults[1][5] << "|" << boardResults[2][5] << "|" << boardResults[3][5] << "|" << boardResults[4][5] << "|" << boardResults[5][5] << "|" << boardResults[6][5] << "|" << boardResults[7][5];


...which shares the stench of inefficiency of my wall of declarations. I would be okay with using the above code if it worked, but if there's a better way to do things, I would be delighted if someone could enlighten me. I really enjoy programming and want to get better at it, but I am rather stuck at the moment. I can also post the code that populates the other array with one "ship square" per column, but it seems to be working as planned. Once the boardResults array can store the coordinate designations of the squares, the code which takes in the square coordinates of the square being attacked will update boardResults with X's or M's, so the rest of the project should be alright. I would appreciate any and all help, this little nubcake is quite lost. =]
You declare your array to hold 7 objects.
You are trying to access the eigth one.

Learn about loops http://cplusplus.com/doc/tutorial/control/
Hi Tesuch,

IF you read a value in, you should check that it is valid. What if the user entered a negative number or 10 say? Also think about the type you have for xCoord and yCoord - perhaps unsigned would be better. If you check the validity after input you can find out about errors and do something about it before the program gets to a line of code that causes it to segfault.

The other problem is that your data structure only holds the grid reference eg "C3", there is no room for any other information. You have mentioned that you have another array to hold the other information, but wouldn't it be be better to have one array with all the info, that way you could loop through once and carry out all the actions, rather than deal with several arrays.

The simplest way to do this, is to have an array of structs. The struct stores the info required at each grid point as members of the struct. you could use an enum to make the letters A to H correspond to the unsigned array index.

Read up about structs, enums and for loops.

Let us know how you go.

TheIdeasMan
Hi Tesuch,

I can see that your program is really a C program that uses cout etc, so I didn't mention that a better way might be to uses classes - if you really want want to do proper object oriented programming. However that it is a bit more complicated, so I went with the array of structs solution.

If you want to do OOP, then you have quite a bit of reading to do. :)

Cheers

TheIdeasMan
Topic archived. No new replies allowed.