hi

im trying to make a tic-tac-toe game in c++
im using a 2-d array of chars to tell if there is an x, 0, or null
i wanted to make a procedure i can keep calling every time i need to display the grid

however i keep getting this error:
invalid conversion from `char' to `char (*)[2]'

#include <iostream>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <wincon.h>
using namespace std;

void Print_Grid(int i, int b, char Grid_pos[][2])
{
    for (b = 0; b < 3; b++)
    {        
       i = 0;
        cout << "||" << Grid_pos[b][i] << Grid_pos[b][i] << Grid_pos[b][i] << "||" << Grid_pos[b][i +1] << Grid_pos[b][i+1] << Grid_pos[b][i+1] << "||" << Grid_pos[b][i+2] << Grid_pos[b][i+2] << Grid_pos[b][i+2] << "||";
        cout << "\n||" << Grid_pos[b][i] << Grid_pos[b][i] << Grid_pos[b][i] << "||" << Grid_pos[b][i +1] << Grid_pos[b][i+1] << Grid_pos[b][i+1] << "||" << Grid_pos[b][i+2] << Grid_pos[b][i+2] << Grid_pos[b][i+2] << "||";
        cout << "\n||" << Grid_pos[b][i] << Grid_pos[b][i] << Grid_pos[b][i] << "||" << Grid_pos[b][i +1] << Grid_pos[b][i+1] << Grid_pos[b][i+1] << "||" << Grid_pos[b][i+2] << Grid_pos[b][i+2] << Grid_pos[b][i+2] << "||";
        cout << "\n||=============||";
        cout << "\n";
    }
}

int main()
{
   //SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10 );

    int i = 0, b = 0, decider = 0, User_Color_Chooser = 0;
    srand(time(NULL));
    bool Grid[3][3];
    char Grid_pos[3][3];
    
    for (b = 0; b < 3; b++)
        {
        for (i = 0; i < 3; i++)
        {
            Grid[b][i] = false;
            }
        }

    for (b = 0; b < 3; b++)
    {
        for (i = 0; i < 3; i++)
        {
            decider = (rand() % 5);
            if (decider == 0)
            {
                   Grid_pos[i][b] = 'x';
                   }
            else if (decider == 1)
             {
                   Grid_pos[i][b] = 'o';
                   }
            else
            {
            Grid_pos[i][b] = ' '; 
                   }         
         }
    }
    cout << "||=============||\n";
    cout << "\n\n\n\n\n\n\n\n\n\n";
    
    Print_Grid ( i,  b,  Grid_pos[2][2]); 
    
    system("pause");
    return (0);
}

this is the line thats giving me the error:
Print_Grid ( i, b, Grid_pos[2][2]);

i dont know why because i am getting it???

any help would be appreciated

ive searched other forums on the internet where people are doing this game with a 2-d array but none have helped

Recommended Answers

All 2 Replies

Grid_pos[2][2] is a single char but you told Print_Grid to expect an array with second dimension 2. If you want to print the whole grid make the function definition void Print_Grid(int i, int b, char Grid_pos[][3]) and pass in Print_Grid(i,b,Grid_pos); so that you get a pointer to the array. Really i and b are not being used in the function body anyway so you don't need them.

commented: very helpful +0

Grid_pos[2][2] is a single char but you told Print_Grid to expect an array with second dimension 2. If you want to print the whole grid make the function definition void Print_Grid(int i, int b, char Grid_pos[][3]) and pass in Print_Grid(i,b,Grid_pos); so that you get a pointer to the array. Really i and b are not being used in the function body anyway so you don't need them.

ohhh. Thnx sooo much cuz i need to be able to pass that array to many functions (printing, checking, etc.)

thnx alot :)

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.