-2

I am trying to make a tic tac toe game in c++, but I am getting the following errors in my code(Not completed yet). am I passing the arrays to the function correctly [Error] invalid conversion from 'char(*)[3]'to 'char' and [Error] initializing argument 1 of 'void player_input(char)'

#include <iostream>
using namespace std;
void draw_grid(char display[][3]){
    cout<<"Board\n"<<"-------------"<<endl;
    for(int x=0;x<3;x++){
        for(int y=0;y<3;y++){
            cout<<"| "<<display[x][y]<<" ";
        }
        cout<<"|"<<endl<<"-------------";
        cout<<endl;
    }
}
void player_input(char);                                     
int main(){
    char arr[3][3]={
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'}
    };
    draw_grid(arr);
    player_input(arr);
    draw_grid(arr);
    return 0;   
    }
void player_input(char size[][3]){
    char sign;
    char number;
    cout<<"X or O"<<endl;
    cin>>sign;
    cout<<"Choose a number\n";
    cin>>number;
    switch (number){
        case '1':
            size[0][0]=sign;
            break;
        case '2':
            size[0][1]=sign;
            break;
        case '3':
            size[0][3]=sign;
            break;
        case '4':
            size[1][0]=sign;
            break;
        case '5':
            size[1][1]=sign;
            break;
        case '6':
            size[1][2]=sign;
            break;
        case '7':
            size[2][0]=sign;
            break;
        case '8':
            size[2][1]=sign;
            break;
        case '9':
            size[2][2]=sign;
            break;
    }
}
  • 1
    How is the `player_input` declared? `void player_input(char);` What do you pass to it? `char arr[3][3]`. See the problem? – Algirdas Preidžius Jan 20 '21 at 11:49
  • You have `void player_input(char);` and `void player_input(char size[][3]) {...}`. These declarations declare two completely different functions. This is probably not what you want. – n. m. could be an AI Jan 20 '21 at 11:51
  • yes I am passing char arr[3][3] to it, what do mean by how is it declared? – Agitated_Mouse Jan 20 '21 at 11:52
  • @Agitated_Mouse "_yes I am passing char arr[3][3] to it, what do mean by how is it declared?_" You declared it as `void player_input(char);`: as a function accepting a single `char`. Yet, you are trying to pass a `char[3][3]` to it. NOTE: `void player_input(char size[][3])` function definition after the `main`. is a **separate** function, that is, by the nature of being defined after the `main`: not seen by the `main`. Consider learning C++ from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of coding randomly. – Algirdas Preidžius Jan 20 '21 at 11:59

2 Answers2

1
void player_input(char);   

Your function definition doesn't match the one below void player_input(char size[][3]). Before execution, the compiler tries to match each function call with its appropriate definition. It fails as it only sees the one above (which takes a single character), being matched with player_input(arr); with arr being a 2D array char arr[3][3].

You can modify the line above to make it work:

void player_input(char[3][3]);

Since you're using C++, you should ditch static arrays and consider using one of the modern C++ containers like std::vector.

DarthQuack
  • 1,254
  • 3
  • 12
  • 22
0

Here's the fix:

#include <iostream>
using namespace std;
void draw_grid(char display[][3]){
    cout<<"Board\n"<<"-------------"<<endl;
    for(int x=0;x<3;x++){
        for(int y=0;y<3;y++){
            cout<<"| "<<display[x][y]<<" ";
        }
        cout<<"|"<<endl<<"-------------";
        cout<<endl;
    }
}
void player_input(char size[][3]){
    char sign;
    char number;
    cout<<"X or O"<<endl;
    cin>>sign;
    cout<<"Choose a number\n";
    cin>>number;
    switch (number){
        case '1':
            size[0][0]=sign;
            break;
        case '2':
            size[0][1]=sign;
            break;
        case '3':
            size[0][3]=sign;
            break;
        case '4':
            size[1][0]=sign;
            break;
        case '5':
            size[1][1]=sign;
            break;
        case '6':
            size[1][2]=sign;
            break;
        case '7':
            size[2][0]=sign;
            break;
        case '8':
            size[2][1]=sign;
            break;
        case '9':
            size[2][2]=sign;
            break;
    }
}
int main(){
    char arr[3][3]={
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'}
    };
    draw_grid(arr);
    player_input(arr);
    draw_grid(arr);
    return 0;   
    }

You had 2 declarations of function player_input(). Also if you are not declaring the function type inside main(), you need to place the function definition before main().

itsDV7
  • 854
  • 5
  • 12