Convert array element from int to string using itoa

Mar 4, 2013 at 5:11pm
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 Mar 4, 2013 at 5:12pm
Mar 4, 2013 at 5:14pm
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).
Mar 4, 2013 at 5:16pm
Which I can later cast as a char correct?
Mar 4, 2013 at 5:17pm
What are you actually trying to do? Maybe you should be using an array of char instead.
Mar 4, 2013 at 5:20pm
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.
Mar 4, 2013 at 5:23pm
That was obvious. Thanks jlb. Made the change to char, all is well.
Last edited on Mar 4, 2013 at 5:23pm
Mar 4, 2013 at 5:24pm
I recommend you use an array of char instead of of the array of int.

char squares[] = {'1','2','3'...}
Mar 4, 2013 at 5:45pm
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 Mar 4, 2013 at 5:47pm
Mar 4, 2013 at 5:50pm
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.