Arrays printing out random characters.

Hey, I am attempting to build a simple tic-tac-toe program. I haven't been programming long, and appear to have bumped into this problem. This is my code:

void drawGrid(char grid[3][3])
{
    cout << " " << grid[1][1] << " | " << grid[1][2] << " | " << grid[1][3] << endl;
    cout << "---|---|---\n";
    cout << " " << grid[2][1] << " | " << grid[2][2] << " | " << grid[2][3] << endl;
    cout << "---|---|---\n";
    cout << " " << grid[3][1] << " | " << grid[3][2] << " | " << grid[3][3] << endl;
}

int main()
{
    char grid[3][3];
    printHeader(); //I haven't left in the printHeader function as it's just a 
                   //cout and shouldn't be affecting anything.
    drawGrid(grid);


    return 0;
}


When i compile and run I see:

¶ | ═ | @
---|---|---
| x |  
---|---|---
" | | Z

Can anybody point me in the right direction please?

Thanks guys.


Edit:
Also, can any of you help with what to use instead of system("CLS"); to clear the terminal window? I have read all about not using system(), but while searching google, they were the only results that popped up!
Last edited on
your array index is incorrect

grid[3][3] must have only values from 0 to 2 not 3! if there were values greater than 2 if prints a null characters as you can see in your output

the function drawGrid() must be
1
2
3
4
5
6
7
8
void drawGrid(char grid[3][3])
{
    cout << " " << grid[0][0] << " | " << grid[0][1] << " | " << grid[0][2] << endl;
    cout << "---|---|---\n";
    cout << " " << grid[1][0] << " | " << grid[1][1] << " | " << grid[1][2] << endl;
    cout << "---|---|---\n";
    cout << " " << grid[2][0] << " | " << grid[2][1] << " | " << grid[2][2] << endl;
}


Also I recommend windows.h library in clearing the screen. don't use system() else you will see flickers.

search and read setConsoleCursorPosition() function

hope this helps
initialize you array using memset() http://cplusplus.com/reference/clibrary/cstring/memset/
and arrays use 0 as the starting index not 1, so an array with of length 3 has 0, 1, 2 as valid indices.
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
#include <iostream>
#include <string.h>
using namespace std;

void drawGrid(char grid[3][3])
{
    cout << " " << grid[0][0] << " | " << grid[0][1] << " | " << grid[0][2] << endl;
    cout << "---|---|---\n";
    cout << " " << grid[1][0] << " | " << grid[1][1] << " | " << grid[1][2] << endl;
    cout << "---|---|---\n";
    cout << " " << grid[2][0] << " | " << grid[2][1] << " | " << grid[2][2] << endl;
}

int main()
{
    char grid[3][3];
    for(int x=0; x<3; x++)
        memset(grid[x], 'x', 3);

    //printHeader(); //I haven't left in the printHeader function as it's just a
                   //cout and shouldn't be affecting anything.
    drawGrid(grid);


    return 0;
}


for clearing the screen
http://cplusplus.com/forum/articles/10515/
I have changed all the numbers as you stated, but now I appear to get the printout of:

 B | N | ├
---|---|---
 w | ý | ═
---|---|---
 @ | x | x



Full code: (Just incase there's something i'm missing?)

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
#include <iostream>
using namespace std;

void printHeader()
{
    cout << "Tic Tac Toe V0.01\n";
    cout << "By Liam\n";
}

void drawGrid(char grid[3][3])
{
    cout << " " << grid[0][0] << " | " << grid[0][1] << " | " << grid[0][2] << endl;
    cout << "---|---|---\n";
    cout << " " << grid[1][0] << " | " << grid[1][1] << " | " << grid[1][2] << endl;
    cout << "---|---|---\n";
    cout << " " << grid[2][0] << " | " << grid[2][2] << " | " << grid[2][2] << endl;
}

int main()
{
    char grid[3][3];
    printHeader();
    drawGrid(grid);
    return 0;
}


I will look into the setConsoleCursorPosition() now.

Thanks very much.

Edit:

Thanks blackcoder41, I will look into those now.
Last edited on
Of course it prints null characters!

you must initialize all the values for grid[3][3]

try the code posted by blackcoder41

include the line
1
2
3
    char grid[3][3];
    for(int x=0; x<3; x++)
        memset(grid[x], 'x', 3);


i have include a goto function to save you from googling
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
#include <iostream>
#include <string.h>
#include <windows.h>
using namespace std;

//gotoxy using windows.h
void gotoxy (int x, int y) {
    COORD coord; // coordinates
    coord.X = x; // X and Y coordinates
    coord.Y = y;
    // moves to the coordinates
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void printHeader() {
    cout << "Tic Tac Toe V0.01\n";
    cout << "By Liam\n";
}

void drawGrid(char grid[3][3]) {
    cout << " " << grid[0][0] << " | " << grid[0][1] << " | " << grid[0][2] << endl;
    cout << "---|---|---\n";
    cout << " " << grid[1][0] << " | " << grid[1][1] << " | " << grid[1][2] << endl;
    cout << "---|---|---\n";
    cout << " " << grid[2][0] << " | " << grid[2][2] << " | " << grid[2][2] << endl;
}

int main() {
    char grid[3][3];
    for (int x=0; x<3; x++)
        memset(grid[x], 'x', 3);//here is the memset() why did you remove it?
    printHeader();
    drawGrid(grid);
    return 0;
}
I had a feeling it was because they weren't initialized, but I wasn't sure how to with the multidimensional arrays. I will make sure I read more into them for future use.

I'm just trying to get my head around that code now :)

Thanks guys.

Edit@blackcoder41 "//here is the memset() why did you remove it?"
I hadn't, I was creating that post before you replied.
Last edited on
ok, hope that helps you anyway.
Topic archived. No new replies allowed.