943,936 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 20043
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 23rd, 2005
0

Need help passing a multi-dimensional array

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sifuedition is offline Offline
25 posts
since Jul 2005
Jul 23rd, 2005
0

Re: Need help passing a multi-dimensional array

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');
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jul 23rd, 2005
0

Re: Need help passing a multi-dimensional array

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..
Reputation Points: 10
Solved Threads: 1
Light Poster
shre86 is offline Offline
33 posts
since Jun 2005
Jul 23rd, 2005
0

Re: Need help passing a multi-dimensional array

Quote 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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void draw(char, int, int);/*
  7. void assign(const string, int, string, int, string, int);
  8. void test(const string, int, int, bool);
  9. */
  10. int main()
  11. {
  12. //string ans = " ";
  13. //while (ans != "y")
  14. //{
  15. //Declare and initialize the array
  16. char empty = ' ';
  17. const int columns = 3;
  18. const int rows = 3;
  19. char tictac[rows][columns];
  20. for (int i = 0; i < rows; i++)
  21. {
  22. for (int j = 0; j < columns; j++)
  23. {
  24. tictac[i][j] = empty;
  25. }
  26. }
  27.  
  28. int count = 1;
  29. bool done = false;
  30. cout << "Let's play tic-tac-toe!" << endl;
  31. draw(tictac, rows, columns);
  32. /*
  33. while (count < 10)
  34. {
  35. test(tictac, rows, done);
  36. if (!done)
  37.   {
  38.   if (count % 2 != 0)
  39.   {
  40.   int col = 0;
  41.   int row = 0;
  42.   string colpick;
  43.   int rowpick;
  44.   cout << "Player X. " << endl;
  45.   cout << "Please enter the column for your move(a, b, or c): ";
  46.   cin >> colpick;
  47.   cout << "Please enter the row for your move(1, 2, or 3): ";
  48.   cin >> rowpick;
  49.   assign(tictac, rows, colpick, rowpick, "X", count);
  50.   }
  51.   else
  52.   {
  53.   int col = 0;
  54.   int row = 0;
  55.   string colpick;
  56.   int rowpick;
  57.   cout << "Player O. " << endl;
  58.   cout << "Please enter the column for your move(a, b, or c): ";
  59.   cin >> colpick;
  60.   cout << "Please enter the row for your move(1, 2, or 3): ";
  61.   cin >> rowpick;
  62.   assign(tictac, rows, colpick, rowpick, "O", count);
  63.   }
  64.   draw(tictac, rows);
  65.   }
  66. }
  67.  
  68. cout << "Would you like to play again? ";
  69. cin >> ans;
  70. }
  71. */
  72. return 0;
  73. }
  74. /*
  75. void assign(const string v[][3], int size, string colpick, int rowpick, string play, int& count)
  76. {
  77. int col;
  78. int row;
  79. if (colpick == "a")
  80.   col = 0;
  81. else if (colpick == "b")
  82.   col = 1;
  83. else
  84.   col = 2;
  85.  
  86. if (rowpick == 1)
  87.   row = 0;
  88. else if (rowpick == 2)
  89.   row = 1;
  90. else
  91.   row = 2;
  92.  
  93. if (v[col][row] != " ")
  94.   cout << "That square is already taken. Please chose another.";
  95. else
  96.   {
  97.   v[col][row] = play;
  98.   count++;
  99.   }
  100. }
  101. */
  102. void draw(char v[][3], int size)
  103. {
  104. cout << endl;
  105. cout << endl;
  106. cout << " " << v[0][0] << " | " << v[0][1] << " | " << v[0][2] << " " << endl;
  107. cout << "---+---+---" << endl;
  108. cout << " " << v[1][0] << " | " << v[1][1] << " | " << v[1][2] << " " << endl;
  109. cout << "---+---+---" << endl;
  110. cout << " " << v[2][0] << " | " << v[2][1] << " | " << v[2][2] << " " << endl;
  111. cout << endl;
  112. cout << endl;
  113. }
  114. /*
  115. void test(const string v[][3], int size, bool& done)
  116. {
  117. if ((v[0][0] == v[1][0] && v[1][0] == v[2][0]) && v[0][0] != " ")
  118. {
  119.   cout << "****You win!*****";
  120.   done = true;
  121. }
  122. if ((v[0][1] == v[1][1] && v[1][1] == v[2][1]) && v[0][1] != " ")
  123. {
  124.   cout << "****You win!*****";
  125.   done = true;
  126. }
  127. if ((v[0][2] == v[1][2] && v[1][2] == v[2][2]) && v[0][2] != " ")
  128. {
  129.   cout << "****You win!*****";
  130.   done = true;
  131. }
  132.  
  133. if ((v[0][0] == v[0][1] && v[0][1] == v[0][2]) && v[0][0] != " ")
  134. {
  135.   cout << "****You win!*****";
  136.   done = true;
  137. }
  138. if ((v[1][0] == v[1][1] && v[1][1] == v[1][2]) && v[1][0] != " ")
  139. {
  140.   cout << "****You win!*****";
  141.   done = true;
  142. }
  143. if ((v[2][0] == v[2][1] && v[2][1] == v[2][2]) && v[2][0] != " ")
  144. {
  145.   cout << "****You win!*****";
  146.   done = true;
  147. }
  148.  
  149. if ((v[0][0] == v[1][1] && v[1][1] == v[2][2]) && v[0][0] != " ")
  150. {
  151.   cout << "****You win!*****";
  152.   done = true;
  153. }
  154. if ((v[0][2] == v[1][1] && v[1][1] == v[2][0]) && v[0][2] != " ")
  155. {
  156.   cout << "****You win!*****";
  157.   done = true;
  158. }
  159. }
  160. */
Reputation Points: 10
Solved Threads: 0
Light Poster
sifuedition is offline Offline
25 posts
since Jul 2005
Jul 23rd, 2005
0

Re: Need help passing a multi-dimensional array

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sifuedition is offline Offline
25 posts
since Jul 2005
Jul 23rd, 2005
0

Re: Need help passing a multi-dimensional array

What's the compiler error?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jul 23rd, 2005
0

Re: Need help passing a multi-dimensional array

Quote 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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sifuedition is offline Offline
25 posts
since Jul 2005
Jul 23rd, 2005
0

Re: Need help passing a multi-dimensional array

>void draw(char, int, int);
char is not the same as a two dimensional array of char.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 23rd, 2005
0

Re: Need help passing a multi-dimensional array

Quote 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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sifuedition is offline Offline
25 posts
since Jul 2005
Jul 23rd, 2005
0

Re: Need help passing a multi-dimensional array

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
sifuedition is offline Offline
25 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Subclassed Editbox Control: Do I need to create 2 subclasses for two edit controls?
Next Thread in C++ Forum Timeline: C++ question on alternating sums





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC