954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help passing a multi-dimensional array

I am actually having two problems. The first is initializing a char array to a single space for all elements. I have the loop to do this but the assignment to the value: " " is returning a compile error: error: invalid conversion from `const char*' to `char'. Then, when I am passing the array to a funtion, I get the error message: error: invalid conversion from `char (*)[3]' to `char'. Any help would be greatly appreciated. I can try to copy code here as well if it will help.

sifuedition
Light Poster
25 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Post the code. However, the error you are getting in your array is that you're probably trying to assign " " rather than ' ' for a single character.

You can also use memset ( variable, ' ', size ); or if you are using C++ and the string class you, you can initialize it with a character and a length to fill it with.
(ie:
string str7 (10,'A');

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

u cant assign an element at each position as u r doing.. u doing it like this rite..??
a[3]="a";
pls post the code..
the part where u re passing the parameter u have to receive a pointer.. then everything should be fixed i think.. anyway better u post the code.. its easier to fix problems tht way..

shre86
Light Poster
33 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 
Post the code. However, the error you are getting in your array is that you're probably trying to assign " " rather than ' ' for a single character. You can also use memset ( variable, ' ', size ); or if you are using C++ and the string class you, you can initialize it with a character and a length to fill it with. (ie: string str7 (10,'A');


Thanks for the single quote tip. That solved one of the problems. This is actually a larger program that is supposed to play tic-tac-toe. I have commented out most of the program in order to solve problems one piece at a time. Anyways, here is the code.

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

void draw(char, int, int);/*
void assign(const string, int, string, int, string, int);
void test(const string, int, int, bool);
*/
int main()
{
//string ans = " ";
//while (ans != "y")
//{
//Declare and initialize the array
char empty = ' ';
const int columns = 3;
const int rows = 3;
char tictac[rows][columns];
for (int i = 0; i < rows; i++)
{
  for (int j = 0; j < columns; j++)
    {
    tictac[i][j] = empty;
    }
}

int count = 1;
bool done = false;
cout << "Let's play tic-tac-toe!" << endl;
draw(tictac, rows, columns);
/*
while (count < 10)
{
test(tictac, rows, done);
if (!done)
  {
    if (count % 2 != 0)
      {
        int col = 0;
        int row = 0;
        string colpick;
        int rowpick;
        cout << "Player X.  " << endl;
        cout << "Please enter the column for your move(a, b, or c):  ";
        cin >> colpick;
        cout << "Please enter the row for your move(1, 2, or 3):  ";
        cin >> rowpick;
        assign(tictac, rows, colpick, rowpick, "X", count);
      }
    else
      {
        int col = 0;
        int row = 0;
        string colpick;
        int rowpick;
        cout << "Player O.  "  << endl;
        cout << "Please enter the column for your move(a, b, or c):  ";
        cin >> colpick;
        cout << "Please enter the row for your move(1, 2, or 3):  ";
        cin >> rowpick;
        assign(tictac, rows, colpick, rowpick, "O", count);
      }
    draw(tictac, rows);
  }
}

cout << "Would you like to play again? ";
cin >> ans;
}
*/
      return 0;
}
/*
void assign(const string v[][3], int size, string colpick, int rowpick, string play, int& count)
{
int col;
int row;
if (colpick == "a")
  col = 0;
else if (colpick == "b")
  col = 1;
else
  col = 2;

if (rowpick == 1)
  row = 0;
else if (rowpick == 2)
  row = 1;
else
  row = 2;

if (v[col][row] != " ")
  cout << "That square is already taken.  Please chose another.";
else
  {
    v[col][row] = play;
    count++;
  }
}
*/
void draw(char v[][3], int size)
{
cout << endl;
cout << endl;
cout << " " << v[0][0] << " | " << v[0][1] <<  " | " << v[0][2] << " " << endl;
cout << "---+---+---" << endl;
cout << " " << v[1][0] << " | " << v[1][1] <<  " | " << v[1][2] << " " << endl;
cout << "---+---+---" << endl;
cout << " " << v[2][0] << " | " << v[2][1] <<  " | " << v[2][2] << " " << endl;
cout << endl;
cout << endl;
}
/*
void test(const string v[][3], int size, bool& done)
{
if ((v[0][0] == v[1][0] && v[1][0] == v[2][0]) && v[0][0] != " ")
{
  cout << "****You win!*****";
  done = true;
}
if ((v[0][1] == v[1][1] && v[1][1] == v[2][1]) && v[0][1] != " ")
{
  cout << "****You win!*****";
  done = true;
}
if ((v[0][2] == v[1][2] && v[1][2] == v[2][2]) && v[0][2] != " ")
{
  cout << "****You win!*****";
  done = true;
}

if ((v[0][0] == v[0][1] && v[0][1] == v[0][2]) && v[0][0] != " ")
{
  cout << "****You win!*****";
  done = true;
}
if ((v[1][0] == v[1][1] && v[1][1] == v[1][2]) && v[1][0] != " ")
{
  cout << "****You win!*****";
  done = true;
}
if ((v[2][0] == v[2][1] && v[2][1] == v[2][2]) && v[2][0] != " ")
{
  cout << "****You win!*****";
  done = true;
}

if ((v[0][0] == v[1][1] && v[1][1] == v[2][2]) && v[0][0] != " ")
{
  cout << "****You win!*****";
  done = true;
}
if ((v[0][2] == v[1][1] && v[1][1] == v[2][0]) && v[0][2] != " ")
{
  cout << "****You win!*****";
  done = true;
}
}
*/
sifuedition
Light Poster
25 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

I have tested the draw function by writing it into main. The lines of code work in main I just need to seperate it into a procedure. I assume that the error is caused by the function call being incorrect since this is where the compiler points to an error but I realize that this could just as easily be a problem with the way I have writen the function definition.

sifuedition
Light Poster
25 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

What's the compiler error?

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 
What's the compiler error?

Compiling the program with most of it commented out like it is above, I get the compiler error:
error: invalid conversion from `char (*)[3]' to `char'.
pointing to line 33 which is the function call to draw.

sifuedition
Light Poster
25 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>void draw(char, int, int);
char is not the same as a two dimensional array of char.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>void draw(char, int, int); char is not the same as a two dimensional array of char.


Ok then would this be:
void draw(char[], int, int);

I am not sure what to use then. The instructor has asked us to use the reference at the top and the definition at the end of the program but this is not really covered in the book for arrays or 2d arrays and I am having a hard time finding info on this anywhere. I have searched the net for quite a while to try to find the info I need and that is how I found this site but I have been unable to locate any sight that includes 1. the function declaration 2. the function call and 3. is describing a multi-dimensional array. I can find lots of info for 1 of the 3 points and even a number that cover two of these points but not all three.

sifuedition
Light Poster
25 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Changing the reference to:
void draw(char[], int, int);
I am now recieving a new compiler error,
tictacstart.cpp: In function `int main()':
tictacstart.cpp:33: error: cannot convert `char (*)[3]' to `char*' for argument

`1' to `void draw(char*, int, int)'

Again this is referencing line 33 which is the function call to draw. Any ideas?

sifuedition
Light Poster
25 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>Any ideas?
Yes, you could stick your head in a bucket of jello twice a day and run around the office naked screaming the national anthem at the top of your lungs.

>void draw(char[], int, int);
...An array of char isn't the same as a two dimensional array of char either. How about changing it to:

void draw ( char (*)[3], int, int);

As the error suggests you should do?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

You could also change it to char ** (much like it is often defined as a command line parameter here...)

int main( int argc, char ** argv )

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

I have worked out all of the bugs now. Thank you everyone for the advice (despite some unnecessary grief). I could not find the information anywhere so your help was invaluable. I think I will work on some error checking in the input values and call it done. Thanks again.

sifuedition
Light Poster
25 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>You could also change it to char **
No, you couldn't because the argument is a two dimensional array. The "arrays are converted to pointers" rule only applies to the first dimension, so a[M][N] is equivalent to (*a)[N], but not equivalent to **a.

>much like it is often defined as a command line parameter here...
No, not really. argv and what the OP is trying to do are two completely different things.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You