Convert array element from int to string using itoa

Is it possible to turn a specific array element where the initial array is defined as a int, into a character or string?

For example:
1
2
int squares[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
squares[1] = 'X';


Is this possible with the itoa function?
Last edited on
Is it possible to turn a specific array element where the initial array is defined as a int, into a character or string?

No, an int array can only hold int values. But note, you can assign 'X' to your int array because a char is an integral type. It won't hold X but it will hold it's numeric value (88).
Which I can later cast as a char correct?
What are you actually trying to do? Maybe you should be using an array of char instead.
Tic tac toe. It would need to change the value of squares every time player 1 or player 2 moves, from the integer of the block they selected, to their corresponding symbol (X or O) to mark the block.
That was obvious. Thanks jlb. Made the change to char, all is well.
Last edited on
I recommend you use an array of char instead of of the array of int.

char squares[] = {'1','2','3'...}
What is wrong with this code? It's saying needs to be a struct left of the '.' size

1
2
3
4
5
6
7
8
9
10
11
struct player {
	char move;
}p1[9], p2[9];

void chickenDinner(bool& winner) {		// Winner, Winner, Chicken Dinner - Check
	for (int i=0; i<p1.size(); i++) {
		if (p1[i].move == 'X' && p1[i+1].move == 'X' && p1[i+2].move == 'X'){
			winner = true;
		}
	}
}
Last edited on
In the following snippet:
for (int i=0; i<p1.size(); i++)
p1 is an array, arrays don't have a size() member function.
Topic archived. No new replies allowed.