This program runs fine.
int main()
{
{
printf("Type something:\n");
char* message = malloc(64 * sizeof(char));
fgets(message, 64, stdin);
printf("message ist : %s\n", message);
free(message);
}
}
But when i run the following program, It doesnt let me write anything, it prints "message ist: "
int main()
{
char action;
while(action!='e')
{
printf("print a line: p\n");
printf("End Program: e\n");
action = getc(stdin);
if(action == 'p')
{
fflush(stdin);
printf("Type something:\n");
char* message = malloc(64 * sizeof(char));
fgets(message, 64, stdin);
printf("message ist : %s\n", message);
free(message);
}
else if(action == 'e')
{
printf(" Program ended successfully\n");
exit(0);
}
}
}
Does anyone have explaination why it let me input in first program, and why it didn't let me input in second program?
I tried to flush the keyboard inputs, it didn't work.
I tried with getline()
instead of fgets()
, same result.
I would be thankful for any ideas and explaination.