-1

I am creating my first project - a chess game. Right now, I have the program ask the user for a row and then a column, like so:

White move.
column: F
row: 3

However, I would like to make this a bit more seamless, so that it looks like the following:

White move.
Input: F3

Then, it takes the 'F' and puts it in a variable, and takes the '3' and puts that in a separate variable. I see solutions to this in other languages, but I don't see it for C. Any ideas?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

2 Answers2

1

The answer of @limserhane is perfectly fine. I just wanted to let you know that is better if you put a space before %c to discard any space in the buffer that can be there either because of a typo from the user or because you didn't fully read previous inputs.

And you should also care about possible errors from the user, looping the scanf until you don't get a valid input.

while( scanf(" %c%d", &c, &i) != 2 || c < 'A' || c > 'Z' || i < 1 || i > 8 ) puts("Invalid coordinates, please try again.");

And also mind to clear the buffer in case the user entered more than you could read with %c%d

while( scanf(" %c%d", &c, &i) != 2 || c < 'A' || c > 'Z' || i < 1 || i > 8 ) {

    while ((c = getchar()) != '\n'); // clear the buffer

    puts("Invalid coordinates, please try again.");
}
anotherOne
  • 1,513
  • 11
  • 20
  • Note: `while( scanf(" %c%d", &c, &i) != 2 || ...` is an infinite loop on end-fo-file. – chux - Reinstate Monica Jan 11 '21 at 17:20
  • @chux-ReinstateMonica why are you saying that? It stops when the user enter for example `A 1` – anotherOne Jan 11 '21 at 17:22
  • "when the user enter for example A 1" is not an "end-of-file" condition. When `stdin` is closed ([end-of-file](https://stackoverflow.com/q/3197025/2410359)), `scanf()` continually returns `EOF`, hence an infinite loop. – chux - Reinstate Monica Jan 11 '21 at 17:51
  • But the user need to enter the end-of-file. Nobody told him to do that – anotherOne Jan 11 '21 at 19:05
  • Best to not assume user input is as expected. Robust code handles all sorts of user inputs, detects problems and reports them. It depends on how resilient your want the code to be. – chux - Reinstate Monica Jan 11 '21 at 19:12
  • Yes but the end-of-file in stdin would be a sentinel value used to say "I'm done". Since the program doesn't mention any sentinel value, why shouldn't the program treat the end-of-file as an invalid input? – anotherOne Jan 11 '21 at 19:21
  • "end-of-file in stdin would be a sentinel value used to say "I'm done". " --> but code does not end the loop on end-of-file. The code here `while( scanf(" %c%d", &c, &i) != 2 ...` repeatedly calls `scanf()` when `scanf()` returns `EOF` due to end-of-file and that is not 2. Then subsequent calls also return `EOF` --> infinite loop. – chux - Reinstate Monica Jan 11 '21 at 19:28
  • Then I should treat `A 1 @` as a correct input as well. Because the user may think that `@` is a sentinel value, even though the program never said anything about that. – anotherOne Jan 11 '21 at 19:44
0

You can use a basic scanf :

char c;
int i;
scanf("%c%d", &c, &i);
printf("column\t%c\nrow\t%d\n", c, i);
limserhane
  • 1,022
  • 1
  • 6
  • 17
  • Thank you so much! I was separating this into two separate scanf functions (one for an int and one for a char), and I did not know you could do this. Thank you! – Waltgrace83 Jan 11 '21 at 16:59