I'm having trouble with sscanf
and fgets
where it seems to be getting the last value input and re reading it back through even though we have come to the end of the file. My code:
while (won == 0) {
char command, input[MAX_LENGTH];
fgets(input, MAX_LENGTH, stdin);
sscanf(input, " %c\n", &command);
printf('%c\n', command);
check_won();
}
Your sscanf
pattern is %c %s
, but you're only ever reading command
. Is that intentional? You should consider checking the return value of fgets
to ensure that it is still actually reading input, and the return value of sscanf
to ensure that it is indeed reading two elements (and discarding the second one). If your input is not being parsed and collected correctly, and your check_won
function is dependent on that input, you will see repeated input because your array will not be being re-initialized.
Try something like this instead?
if(fgets(input, MAX_LENGTH, stdin) == NULL) {
break;
}