I was making a dos based text game and needed user input. I looked through the forums and found the thread on safe user input, which would be using fgets() instead of gets().
The first problem I ran into was that stdin always had '\n' in the buffer, so it wouldn't wait for user input. I read online that stdin has to be cleared of all '\n' characters before calling it, so I used scanf() to remove the newline characters from stdin.
I am now stumped with why the following code doesn't get the user's input.
/* clear stdin */
i_eof = scanf("%*[^\n]"); /* read and discard all non-newlines */
if ( i_eof != EOF)
{
i_eof = scanf("%*c");
}
/* get user input */
fgets( m_c_inputBuffer_ary, i_InputMax, stdin );
I have attemped discarding the scanf calls and using fflush(stdout) followed by fflush(stdin), but that brings me to the same problem as before. the fgets() call won't wait for user input because of a newline character in stdin.
Placing a fflush(stdin) call after the scanf() calls and before the fgets() calls also yields the result of pausing for user input, but getting nothing as the value of the input.
Interested in finding out what's going on.
Thanks in advanced