User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,750 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,457 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 11829 | Replies: 13
Reply
Join Date: Jul 2005
Location: Okc, OK
Posts: 25
Reputation: sifuedition is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sifuedition's Avatar
sifuedition sifuedition is offline Offline
Light Poster

Help Need help passing a multi-dimensional array

  #1  
Jul 23rd, 2005
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Posts: 461
Reputation: winbatch is on a distinguished road 
Rep Power: 4
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Need help passing a multi-dimensional array

  #2  
Jul 23rd, 2005
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');
Reply With Quote  
Join Date: Jun 2005
Posts: 33
Reputation: shre86 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
shre86 shre86 is offline Offline
Light Poster

Re: Need help passing a multi-dimensional array

  #3  
Jul 23rd, 2005
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..
Reply With Quote  
Join Date: Jul 2005
Location: Okc, OK
Posts: 25
Reputation: sifuedition is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sifuedition's Avatar
sifuedition sifuedition is offline Offline
Light Poster

Re: Need help passing a multi-dimensional array

  #4  
Jul 23rd, 2005
Originally Posted by winbatch
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;
}
}
*/
Reply With Quote  
Join Date: Jul 2005
Location: Okc, OK
Posts: 25
Reputation: sifuedition is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sifuedition's Avatar
sifuedition sifuedition is offline Offline
Light Poster

Re: Need help passing a multi-dimensional array

  #5  
Jul 23rd, 2005
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.
Reply With Quote  
Join Date: Feb 2005
Posts: 461
Reputation: winbatch is on a distinguished road 
Rep Power: 4
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Need help passing a multi-dimensional array

  #6  
Jul 23rd, 2005
What's the compiler error?
Reply With Quote  
Join Date: Jul 2005
Location: Okc, OK
Posts: 25
Reputation: sifuedition is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sifuedition's Avatar
sifuedition sifuedition is offline Offline
Light Poster

Re: Need help passing a multi-dimensional array

  #7  
Jul 23rd, 2005
Originally Posted by winbatch
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.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,070
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Need help passing a multi-dimensional array

  #8  
Jul 23rd, 2005
>void draw(char, int, int);
char is not the same as a two dimensional array of char.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jul 2005
Location: Okc, OK
Posts: 25
Reputation: sifuedition is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sifuedition's Avatar
sifuedition sifuedition is offline Offline
Light Poster

Re: Need help passing a multi-dimensional array

  #9  
Jul 23rd, 2005
Originally Posted by Narue
>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.
Reply With Quote  
Join Date: Jul 2005
Location: Okc, OK
Posts: 25
Reputation: sifuedition is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
sifuedition's Avatar
sifuedition sifuedition is offline Offline
Light Poster

Re: Need help passing a multi-dimensional array

  #10  
Jul 23rd, 2005
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?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:16 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC