There is an NxN sized Matrix (N is a number between 2-10) where we can put down chess pieces. You can put only one piece of one field. The program asks for the size of the table then asks for the chess pieces position like this.
1. piece: B2
2. piece: C5
3. piece: D2
...
After the last piece the user can exit from the loop by typing an 'x'.
My problem is that I have to put all this data into one array like this:
char position[100] = {B2C5D2}
This is the code to the function I tried to use:
char inputPosition(char position[]) {
int i = 0;
do {
printf("%d. piece: ", i+1);
scanf("%s", &position[i]);
i++;
}while(position[i-1]!='x');
return position;
}
The output looks like this: (for B2C5D2)
BCDx
I'm not sure I should use scanf and the program shouldn't get the value x as a parameter but I don't know what to do.
EDIT: It's doesn't matter if the position is valid or not in my example. Also I edited the max input of the char positions. (The max variables I have to use is around 10) My real problem is that I can't put every position[i] into one array without spaces.
&position[2 * i]? And you can only store 9 positions in a 20-character array, unless you use" %2c"in place of%s. You'd still not have a null-terminated string with 10 pairs of characters for the position. You're also not checking that the positions are valid.