•
•
•
•
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
![]() |
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.
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');
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');
•
•
•
•
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;
}
}
*/ 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.
•
•
•
•
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.
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?
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?
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the C++ Forum
- Previous Thread: Subclassed Editbox Control: Do I need to create 2 subclasses for two edit controls?
- Next Thread: C++ question on alternating sums



Linear Mode