Compiler error? Tic-tac-toe

Hello,
i'm having some problems to solve this incompleted tic-tac-toe code.
After the compilation you can see that are some strange simbols in the table.
Can someone tell me what is happening ?
if i change screen[3][3] to screen[4][4] the problem is "solved" but the true is that the problem is hide in screen[4][4]

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

using namespace std;

char screen[3][3];
unsigned short int i,j,a;

void ShowScreen();
void AskChoice();
void TestChoice();

int main()
{
    //--------------------------------
    //   Gera a matriz que representa
    //   o tabuleiro
    //--------------------------------
    
    for (i = 0; i<3; i++)
    {
        for(j = 0; j<3; j++)
        {
             screen[i][j] = ' ';
        }
    }
    
    
    
    while(1)
    {
      ShowScreen();
      AskChoice();
      TestChoice();
      screen[i][j]='X';
      system("cls");
    }
    
    
    getchar();
    getchar();
    return 0;
}



//------------------------------
// Função que mostra o tabuleiro
//------------------------------
void ShowScreen()
{
    cout << "\t Tic-Tac-Toe\n\n";
    cout << "\t     " << screen[1][1] << "|" << screen[1][2] << "|" << screen[1][3] << endl;
    cout << "\t     " << screen[2][1] << "|" << screen[2][2] << "|" << screen[2][3] << endl; 
    cout << "\t     " << screen[3][1] << "|" << screen[3][2] << "|" << screen[3][3] << endl;
}




void AskChoice()
{
    cout << "\n\n\t choose the row: ";
    cin >> i;
    cout << "\t choose the column: ";
    cin >> j;
}




void TestChoice()
{
     if (screen[i][j] == 'X')
     { 
       cout << "\nEste Invalid option. Choose another.";
       AskChoice();
     }
}
Last edited on
Neither the first index nor the second index can have value equal to 3. The acceptable range for indexes is [0, 2]. So for example this code is invalid. You are accessing memory beyond the array

cout << "\t " << screen[3][1] << "|" << screen[3][2] << "|" << screen[3][3] << endl;
dynamically allocated things start at index 0 not index 1.
Arrays start at zero, so the declaration of screen[3][3] has valid subscripts 0,1,2.

So the statement screen[3][3] is out of range for the array, and prints garbage values.

Apart from that, you should get out of the habit of using global variables. Declare them in main(), and pass them to any function that needs them. Also check out the use of references.

When using arrays, it is much easier to use nested for loops to interact with them.

1
2
3
4
5
6
7
8
9
10
11
const int SIZE = 3;

char Board[SIZE][SIZE];

// initialise the board to blank spaces
for(int Row = 0; Row < SIZE; Row++) {
     for(int Col = 0; Col < SIZE; Col++) {
        // print  Board[Row][Col]  and formatting here
     }
} 


Have you seen the tutorial & reference section on this site? Links to them are at the top left of this page.

Hope all goes well.
Sure, thank you for the tips, I really going to use it right now.
Actually I have not seen it. Yesterday I asked some guys for some kind of forum where i could ask for some help and one of the guys told me about this site.
Right now I'm for sure going to see the tutorials and references.

By the way thank you guys!
Topic archived. No new replies allowed.