I know questions like this get asked all the time, and I have read several, however, I never use scanf()
in my code like all the other ones, so I cannot find a comparable question. I don't know why on the second, third, fourth and so on iterations, the while loop is skipping my first fgets()
.
Code:
int main()
{
char word[40];
char answer[2];
while(true) {
printf("Please enter word: ");
fgets(word, sizeof(word), stdin);
printf("Your word is: %s\n", word);
printf("Would you like to go again?");
fgets(answer, sizeof(answer), stdin);
printf("%d\n", strcmp(answer, "F"));
if (strcmp(answer, "F") == 0) {
printf("breaking");
break;
} else {
printf("continuing");
continue;
}
}
}
Output:
Please enter word: Hey
Your word is: Hey
Would you like to go again?Y
Please enter word: Your word is:
Would you like to go again?Y
Please enter word: Your word is:
Would you like to go again?Y
...etc.
I figure this has something to do with clearing the input buffer, and I have tried several things, but nothing is working. Second day messing around with C, so I don't understand to much of it. (Python was easier lol)