I'm currently working on a function that takes user input to select a point on a Battleship game board. The rows are represented with the letters A-J while the columns are represented by numbers 0-9. I want the user to be able to type in something like "B5" and then take each component into a separate variable (so B would be stored into a char variable and 5 into an int variable). How can I do this in C?
Asked
Active
Viewed 41 times
0
-
2Please show what you have tried, but my crystal ball says: [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Oct 24 '22 at 18:24
-
The best way to do this depends on how tolerant you want to be of user typing mistakes. – stark Oct 24 '22 at 18:26
-
A good way might be to enter a string with `fgets` and examine the first character and subsequent integer for validity. Then use `fgets` for every other input, whatever its purpose. – Weather Vane Oct 24 '22 at 18:29
-
https://ericlippert.com/2014/03/21/find-a-simpler-problem/ I.e. first do a program which can take a single non-digit character as input. Then write a program that can take a single digit character as input and turn it into an `int` < 10. Then try to combine them. that will probably help you to focus your question on a more specific programming problem and allow you to give an [mre], or two, here. – Yunnosch Oct 24 '22 at 18:42
-
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Oct 25 '22 at 03:15
1 Answers
0
There are probably a lot of ways to approach this, but the following code snippet offers up a proof-of-principle example using the entry of the coordinates as a string and then evaluating the two alphanumeric characters entered.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_coordinate(char * xy)
{
if (xy[0] < 'A' || xy[0] > 'J')
{
return 0;
}
if (xy[1] < '0' || xy[1] > '9')
{
return 0;
}
printf("The first portion of the coordinate is: %c, the second portion of the coordinate is: %c\n", xy[0], xy[1]);
return 1;
}
int fire(char * xy)
{
/* Function to see if there was a hit or a miss */
return 0;
}
int main()
{
char xy[4];
int result = 0;
while (1)
{
printf("Enter coordinate to fire upon: ");
scanf("%s", xy);
if (strcmp(xy, "End") == 0)
{
break;
}
if (check_coordinate(xy) == 0)
{
printf("An invalid coordinate was entered - try again\n");
}
else
{
result = fire(xy);
}
}
return 0;
}
Here is some sample output noting when an invalid coordinate was entered as opposed to a valid coordinate.
@Dev:~/C_Programs/Console/Battleship/bin/Release$ ./Battleship
Enter coordinate to fire upon: C8
The first portion of the coordinate is: C, the second portion of the coordinate is: 8
Enter coordinate to fire upon: K7
An invalid coordinate was entered - try again
Enter coordinate to fire upon: D4
The first portion of the coordinate is: D, the second portion of the coordinate is: 4
Enter coordinate to fire upon: End
Ultimately, you might come up with a different approach to checking coordinates, but this should give you some food for thought.

NoDakker
- 3,390
- 1
- 10
- 11
-
Consider what this program does if the user types `Hello World!`. (The behavior is undefined). – William Pursell Oct 24 '22 at 21:52
-
Yes. Good catch. Probably would want to enlarge the size to something like 64. – NoDakker Oct 24 '22 at 22:08
-
If you increase the size to 64, you have the same problem when the input is 70 characters long. You need a width modifier: `scanf("%3s", xy);` – William Pursell Oct 24 '22 at 22:16