Validation for 2D Char Array Insertion

I am working on Tic Tac Toe, and I am mostly complete. This is a side program snippet of code to avoid messing up the other program, but its essentially the same idea. I am trying to make the validation part so the condition of the if statement evaluates if the spot is empty or not, by having the char space in there, ' '. This does not seem to be working for me, can anyone help?


Also, I am trying to get some ideas about how to go about programming when someone wins. Should I do this with a bunch of if statements and include all possible combinations of a win? This seems to be an impractical approach, so please excuse my novice programming.

Many thanks.




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

#include <iostream>

using namespace std;
int main()
{
    int row, column = 0;
    char choice = ' ';
    char basic2DArray[2][2] = {{' ',' '},
                               {' ',' '}};
    
//Prints out 2D array
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << "|" << basic2DArray[i][j] << " ";
        }
        cout << endl;
    }

    //Insert element into array with rows & column entry
    cout << "1Row: "; cin >> row;
    cout << "1Column: "; cin >> column;
    

//If statement checks if spot is empty or not
    if (basic2DArray[row][column] != ' ')
    {
        cout << "1Error! Spot already taken!";
    }
    cout << "1Enter any random character: "; cin >> choice;
    basic2DArray[row-1][column-1] = choice;

    
    
    
    
    
    
    
    
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << "|" << basic2DArray[i][j] << " ";
        }
        cout << endl;
    }
    
    
    
    
    
    
    
    
    
    
    
    cout << "2Row: "; cin >> row;
    cout << "2Column: "; cin >> column;
    if (basic2DArray[row][column] != ' ')
    {
        cout << "Error! Spot already taken!";
    }
    cout << "2Enter any random character: "; cin >> choice;
    basic2DArray[row-1][column-1] = choice;

    
    
    
    
    
    
    
    
    
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << "|" << basic2DArray[i][j] << " ";
        }
        cout << endl;
    }
    
    
    
    
    
    
    
    
    
    
    
    cout << "3Row: "; cin >> row;
    cout << "3Column: "; cin >> column;
   if (basic2DArray[row][column] != ' ')
    {
        cout << "Error! Spot already taken!";
    }
    cout << "3Enter any random character: "; cin >> choice;
    basic2DArray[row-1][column-1] = choice;

    
    

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cout << basic2DArray[i][j] << " ";
        }
        cout << endl;
    }
    
    
        
}
This does not seem to be working for me, can anyone help?

What does "not working" mean?

1
2
3
4
5
6
7
    if (basic2DArray[row][column] != ' ')
    {
        cout << "1Error! Spot already taken!";
    }
    cout << "1Enter any random character: ";
    cin >> choice;
    basic2DArray[row-1][column-1] = choice;

You're checking basic2DArray[row][column] but then modifying basic2DArray[row-1][column-1]. This doesn't match. Is that intended?

Also, if the users enters {row=0, column=0}, you're going to be accessing basic2DArray[-1][-1], which is definitely not a valid index.
Last edited on
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
#include <iostream>
#include <algorithm>

// UTF-8 box-drawing characters
const char *Horz = "\xE2\x94\x80";
const char *Vert = "\xE2\x94\x82";
const char *Cross = "\xE2\x94\xBC";
/*
// DOS box-drawing characters
const char *Horz = "\xC4";
const char *Vert = "\xB3";
const char *Cross = "\xC5";
*/

const int Size = 3;

void print(char a[][Size]) {
    for (int i = 0; i < Size; i++) {
        for (int j = 0; j < Size - 1; j++)
            std::cout << ' ' << a[i][j] << ' ' << Vert;
        std::cout << ' ' << a[i][Size - 1] << '\n';
        if (i < Size - 1) {
            for (int j = 0; j < Size - 1; j++)
                std::cout << Horz << Horz << Horz << Cross;
            std::cout << Horz << Horz << Horz << '\n';
        }
    }
}

void set(char a[][Size]) {
    int row, col = 0;
    char choice = ' ';
    while (true) {
        std::cout << "Row: ";
        std::cin >> row;
        std::cout << "Column: ";
        std::cin >> col;
        if (row < 1 || row > Size || col < 1 || col > Size)
            std::cout << "Row and column must be from 1 to " << Size << '\n';
        else if (a[row-1][col-1] != ' ')
            std::cout << "That spot is already taken!\n";
        else
            break;
    }
    std::cout << "Enter a character: ";
    std::cin >> choice;
    a[row-1][col-1] = choice;
}

int main() {
    char a[Size][Size];
    std::fill(&a[0][0], &a[Size][0], ' ');
    for (int i = 0; i < 3; i++) {
        print(a);
        set(a);
    }
}

Topic archived. No new replies allowed.