I did include the library <stdio.h>
but the codes below leads to an infinite loop with VS 2015 (as well as GNU GCC):
//Sample Code
void main() {
char check;
do {
check = '\0';
printf("Enter a character > ");
fflush(stdin);
scanf("%c", &check);
if (check == '\n') printf("\nERROR\n");
} while (check != '\n');
system("pause"); }
/*
Result:
Enter a character > r
Enter a character >
ERROR
Enter a character > ww
Enter a character > Enter a character >
ERROR
*/
But when I compiled it in my previous version of VS, which was 2013, it worked completely fine. So I have suspected that the fflush
function no longer works with VS 2015.
Sorry, I'm new in C languages.
So, according to what I have leant, using fflush(stdin)
is actually a bad idea and this function has been marked as an undefined behavior.
But here are the real questions which are making me curious:
Why does
fflush(stdin)
no longer work with VS2015?Is there anyway else to implement the "function" of
fflush(stdin)
that can be used in the code involved do-while loop (to prevent infinite loop)?
Thank you