I'm trying to read input from fgets()
and the function doesn't wait for output (from keyboard). We were told to use fgets
so scanf
is not an option here. The fgets
function does work when it's used in the main
function for some reason but not in an additional function.
Here's the code
char name[21]={0x0}; //tried everything; not assigning value and NULL&\0
printf("\nName: ");
//fseek(stdin,0,SEEK_END);
//rewind(stdin);
//fflush(stdin); //these don't help
printf(stdin); //for testing purposes, probably pointless
fgets(nimi, 21, stdin);
The program will skip the fgets()
and not wait for input, and later adds some empty string where I want the name string. However name
does not get the value NULL because there's an error check. I know there's quite a few people having this problem but I didn't find any help. Most people tell to use scanf
or some other function. For the record scanf()
will work as expected here. The syntax should be correct since it works in main()
. I probably don't even know how stdin
actually works so I apologise if the title is silly.
This snippet is directly after a malloc for pointer (if else clause)