1

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.

1
  • 1
    Don't you need to read into &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. Commented Jan 8, 2017 at 1:42

1 Answer 1

2

You need to offset the position in the array to allow for 2 characters per entry, so use &position[2*i] as the argument to scanf().

It's a good idea to tell your code how big the array is, and to ensure that you don't overflow that array (either by indexing off the end or by accepting more characters in total than will fit).

One possible solution is:

#include <stdio.h>

static void inputPosition(int pos_size, char position[pos_size])
{
    for (int i = 0; i < pos_size / 2; i++)
    {
        printf("%d. piece: ", i + 1);
        scanf("%2s", &position[2 * i]);
        if (position[2 * i] == 'x')
        {
            position[2 * i] = '\0';
            break;
        }
    }
}

int main(void)
{
    char position[21];

    inputPosition(sizeof(position), position);
    printf("Position: [%s]\n", position);
    return 0;
}

And a sample run yields:

1. piece: B2
2. piece: C5
3. piece: D2
4. piece: A1
5. piece: E3
6. piece: F8
7. piece: H8
8. piece: A3
9. piece: D4
10. piece: E2
Position: [B2C5D2A1E3F8H8A3D4E2]

Another sample run with early exit is:

1. piece: B2
2. piece: C5
3. piece: D2
4. piece: x
Position: [B2C5D2]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Jonathan, you really helped me out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.