Tic-Tac-Toe Help

Hey, I'm sorry if I say anything "wrong", I'm new here, but on to my question.

I am trying to learn C++, and I thought that making a tic tac toe game would be a good, quick, couple hour project, but I'm having a problem here, occasionally it says that the game has been won by player 1 when it shouldn't have, and I cant get player 2 to win, here is the code, the problem is in the winCheck function.
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
  #include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

void putBoard(char board[3][3])
{
    system("clear;");
    for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 3; ++j)
        {
            cout << board[i][j];
        }
        cout << "\n";
    }
    return;
}
int winCheck(char array[3][3])
{
    char winner;
    for (int i = 0; i < 3; i++)
            if (array[i][0] == array[i][1] and array[i][1] == array[i][2])
                        winner = array[i][0];
    for (int i = 0; i < 3; i++)
            if (array[0][i] == array[1][i] and array[1][i] == array[2][i])
                        winner = array[0][1];
    if ((array[0][0] == array[1][1] and array[1][1] == array[2][2]) or 
                (array[0][2] == array[1][1] and array[1][1] == array[2][0]))
            winner = array[1][1];
    
    /*
     * 0- undecided
     * 1- O wins
     * 2- X wins
     * 3- tie 
    */
    if(winner == 'O') return 1;
    if(winner == 'X') return 2;
    bool tie = false;
    for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 3; ++j)
        {
            if(array[i][j] == ' ') return 0;
        }
    }
    return 3;
}
int main(int argc, char *argv[])
{
    char board[3][3];
    for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 3; ++j)
        {
            board[i][j] = ' ';
        }
    }
    int tempRow;
    int tempCol;
        /*
         * 0- undecided
         * 1- O wins
         * 2- X wins
         * 3- tie 
        */
    while(true)
    {
        putBoard(board);
        cout << "Player 1, O's, turn\n";
        cout << "Please choose a row: ";
        cin >> tempRow;
        cout << "Please choose a column: ";
        cin >> tempCol;
        if(board[tempRow-1][tempCol-1] != 'X')
        {
            board[tempRow-1][tempCol-1] = 'O';
        }
        putBoard(board);
        if(winCheck(board) == 1)
        {
            cout << "Player 1 wins!\n";
            break;
        }
        if(winCheck(board) == 3)
        {
            cout << "Tie\n";
            break;
        }
        cout << "Player 2, X's, turn\n";
        cout << "Please choose a row: ";
        cin >> tempRow;
        cout << "Please choose a column: ";
        cin >> tempCol;
        if(board[tempRow-1][tempCol-1] != 'O')
        {
            board[tempRow-1][tempCol-1] = 'X';
        }
        putBoard(board);
        if(winCheck(board) == 2)
        {
            cout << "Player 2 wins!\n";
            break;
        }
        if(winCheck(board) == 3)
        {
            cout << "Tie\n";
            break;
        }
    }
    return 0;
}
Line 22 - winner is uninitialized. You could fall through lines 23-31 and never set winner. Since winner is uninitialized, it could randomly have an X or O in it and therefore return an improper result at lines 39-40.

Thank you so much AbstractionAnon, it all works now!
Topic archived. No new replies allowed.