TOPIC: fgets hanging inside if statement for string with spaces support
what's wrong with this code? I believe fgets is hanging. I have tried this code in a separate source file and it works:
Code:
#include <stdio.h>
int main (void)
{
FILE *fp;
fp = fopen("groc.txt", "a+");
char my_string[20];
fgets(my_string, 20, stdin);
fputs(my_string, fp);
printf("You entered: ");
puts(my_string);
return 0;
}
This is the modification to the attached (grocery.c). I had scanf functions in here in my original code, this is an old project and i am trying to find out how to pull the old source code from github so I couldnt paste the old code. Anyway, I want support for a string with a space in it (that's why i'm now using fgets) but it's not working correctly inside the if statement but works in it's own separate file, above. i am trying to modify to the add_entry() function. Full source code attached (grocery.c)
Attachment 15986
Code:
if (number == 1)
{
printf("Enter item to add (%d): ", number);
fgets(fridge_string, 20, stdin);
fputs(fridge_string, fp);
printf("You added: ");
puts(fridge_string);
menu();
}