void placeg(game** g){
//place marble;
char row, col;
char* buffer = NULL;
printf("Please enter a move: ");
scanf(" %c%c%s", &row, &col, buffer);
// scanf(" %s", buffer);
pos p = make_pos(charToInt((int)row),charToInt((int)col));
place_marble((*g),p);
board_show((*g)->b);
}
When I run the scanf function above in terminal, I expect to read and take in two char. For example, "AB" would be a valid terminal input. But in this case, I want my code to be able to detect invalid inputs such as "ABC" and inform the user accordingly. The following code above does not work for both valid and invalid inputs but I do not know why. Any insights would be greatly appreciated. In addition how would I potentially be able to detect other kinds of invalid inputs such as "A" or "" and be able to inform user accordingly?
Use
fgets
for input. Parse as needed.sscanf
is but one option for parsing.This uses A through I for valid row and 1 through 9 for valid column. That can be changed for actual requirements.
If
fgets
is used for input, usefgets
for all input. Don't mix withscanf
.