So, basically what I'm trying to do is a Tic Tac Toe game for a project. I started with making 3 1-D Char Arrays to represent the game's bord. the code below compares the Arrays data to try and figure out if any of the winning possibilites were achieved by option x or y. There is a variable set to 0, this variable will change to 1 if player1 won and will change to 2 if player2 won, if it remains 0 then it's a draw. Now, there's 16 errors of Error C2446 '==': no conversion from 'const char [2]' to 'int'.

#include <stdio.h>
#include <conio.h>
#include <string.h>
//This function is made to check the result of the round, either one of the players win or it's a draw. It also states who is the winner or if it's a draw.
int main()
{
    int playerwon = 0;//This variable is to indicate which player won, if it's = 1 then Player1 won, if 2 then player2 won, if it remains 0 then draw.
    char board1[4] = {'-','-','-','\0'};
    char board2[4] = { '-','-','-','\0'};
    char board3[4] = { '-','-','-','\0'};

    //Coloumns win check for X, Player 1.
    if (board1[0] == board2[0] && board3[0] == board2[0] && board3[0] == 'x')
    {
        playerwon = 1;
    }
    else if (board1[1] == board2[1] && board3[1] == board2[1] && board3[1] == 'x')
    {
        playerwon = 1;
    }
    else if (board1[2] == board2[2] && board3[2] == board2[2] && board3[2] == 'x')
    {
        playerwon = 1;
    }
    //Rows win check for X, Player 1.
    else if (board1[0] == board1[1] && board1[1] == board1[2] && board1[2] == 'x')
    {
        playerwon = 1;
    }
    else if (board2[0] == board2[1] && board2[1] == board2[2] && board2[2] == 'x')
    {
        playerwon = 1;
    }
    else if (board3[0] == board3[1] && board3[1] == board3[2] && board3[2] == 'x')
    {
        playerwon = 1;
    }
    //Diagonals win check for X, Player 1.
    else if (board1[0] == board2[1] && board2[1] == board3[2] && board3[2] == 'x')
    {
        playerwon=1;
    }
    else if (board1[2] == board2[1] && board2[1] == board3[0] && board3[0] == 'x')
    {
        playerwon = 1;
    }
    //Coloumns win check for Y, Player 2.
    else if (board1[0] == board2[0] && board3[0] == board2[0] && board3[0] == 'y')
    {
        playerwon = 2;
    }
    else if (board1[1] == board2[1] && board3[1] == board2[1] && board3[1] == 'y')
    {
        playerwon = 2;
    }
    else if (board1[2] == board2[2] && board3[2] == board2[2] && board3[2] == 'y')
    {
        playerwon = 2;
    }
    //Rows win check for Y, Player 2.
    else if (board1[0] == board1[1] && board1[1] == board1[2] && board1[2] == 'y')
    {
        playerwon = 2;
    }
    else if (board2[0] == board2[1] && board2[1] == board2[2] && board2[2] == 'y')
    {
        playerwon = 2;
    }
    else if (board3[0] == board3[1] && board3[1] == board3[2] && board3[2] == 'y')
    {
        playerwon = 2;
    }
    //Diagonals win check for Y, Player 2.
    else if (board1[0] == board2[1] && board2[1] == board3[2] && board3[2] == 'y')
    {
        playerwon = 2;
    }
    else if (board1[2] == board2[1] && board2[1] == board3[0] && board3[0] == 'y')
    {
        playerwon = 2;
    }
    //Printing the result of the round!
    if (playerwon = 1) {
        printf("Player one won this round!");
    }
    else if (playerwon = 2) {
        printf("Player two won this round!");
    }
    else if (playerwon = 0) {
        printf("This round is a draw!");
    }
    else {
        printf("Error calculating the round result!");
    }
    return 0;
}

Dani AI

Generated

Classic C gotcha: two different issues commonly cause the symptoms shown in the thread. fixed the immediate logic bug by switching the assignment operator to a comparison, and offered a compact integer-sum approach as an alternative. The remaining useful checks are about literal types, compiler warnings, and simplifying the win tests so the same slip cannot happen again.

A few diagnostic points to keep in mind: comparing a char to a double‑quoted literal (e.g. "x") triggers type errors because "x" is a string (type const char[2]), not a single char — use single quotes 'x'. An if written as if (playerwon = 1) performs assignment (and evaluates nonzero), so it always succeeds; the comparison operator is ==. Enable warnings to catch these: GCC/Clang -Wall -Wextra, MSVC /W4 (and /WX to treat warnings as errors) will flag accidental assignments in conditionals and many other mistakes.

To reduce repetition and the chance of typos, factor the repeated three-in-a-row check into a small helper rather than writing the same chained expression many times. For example:

#include <stdbool.h>

static inline bool three_same(char a, char b, char c, char mark) {
    return a == mark && b == mark && c == mark;
}

Using a helper like this keeps the logic concise and makes it obvious when comparisons are used (not assignments). Consider also using a single flattened board or integer-coded cells (as suggested) or an enum for clear state values; both approaches make win detection easier to test. Finally, avoid nonstandard headers like <conio.h> for portable code, and add a few unit tests (one winning X, one winning O, one draw) so regressions are caught early.

I have solved the problem, basically what I had to do is

   if (playerwon = 1) {
        printf("Player one won this round!");
    }
    else if (playerwon = 2) {
        printf("Player two won this round!");
    }
    else if (playerwon = 0) {
        printf("This round is a draw!");

Change the playerwon = to Playerwon==, it worked just fine.

use a short code

#include <stdio.h>

int main(){

    // use int array of 9 cells
    // 0 means blank
    // 1 means O (player 1)
    // -1 means X (player 2)
    int board[9] = {    1,   1,   -1, 
                        -1,  1,   1, 
                        1,   -1,  -1        };  

    int row1 = board[0] + board[1] + board[2];
    int row2 = board[3] + board[4] + board[5];
    int row3 = board[6] + board[7] + board[8];

    int col1 = board[0] + board[3] + board[6];
    int col2 = board[1] + board[4] + board[7];
    int col3 = board[2] + board[5] + board[8];

    int dia1 = board[0] + board[4] + board[8];
    int dia2 = board[2] + board[4] + board[6];

    int board_sum[8] = {row1, row2, row3, col1, col2, col3, dia1, dia2};
    int i=0;
    int has_winner=0;

    for(i=0;i<8;i++){
        if (abs(board_sum[i])==3){  
        // if absolute value of any of the sum is 3, it means we have a winner
            has_winner=1;

            if (board_sum[i]>0){
                printf("Player 'O' won this round!");
            } else{
                printf("Player 'X' won this round!");
            }
            break;
        }  
    }
    if (has_winner==0){ printf("This round is a draw!");}

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.