clearing buffer after reading string w/ scanf()
Why do I have to clear the input buffer after reading a string with scanf()?
Why do I have to do this to keep may console screen from disappearing:
Code:
#include <stdio.h>
int main()
{
char string_ch[50];
printf("please enter a string: ");
scanf("%s", string_ch);
while (getchar() != '\n');
printf("please press enter");
getchar();
return 0;
}
instead of this?
Code:
#include <stdio.h>
int main()
{
char string_ch[50];
printf("please enter a string: ");
scanf("%s", string_ch);
printf("please press enter");
getchar();
return 0;
}