> Isn't that getchar? Pressing '1' with the code I have jumps straight to the second function... just too quickly, meaning the user is still pressing '1' when asked to input a string.
getch() is unbuffered input and fgets is buffered input.
There is no reason to assume that these two input streams are synchronised - they appear to be in DJGPP, but for your compiler, it looks as if they are not.
Code:
#include <stdio.h>
#include <conio.h>
void foo ( ) {
char buff[10];
printf( "Enter name > " );
fflush( stdout );
fgets( buff, sizeof(buff), stdin );
printf( "Name=%s", buff );
}
int main ( ) {
int ch;
printf( "Press '1' > " );
fflush( stdout );
ch = getch();
if ( ch == '1' ) foo();
return 0;
}
I can press the keys "1fred\n", and get this output
Press '1' > Enter name > fred
Name=fred