I used the fflush() in Linux GCC but it did not work. Are there any alternatives for that function? Here is my code:
#include
void main()
{
#include<stdio.h>
int main()
{
char ans='y';
int a;
while(ans=='y'||ans=='Y')
{
printf("Type a number:-");
scanf("%d",&a);
printf("square of number = %d\nwant to enter
number again(y/n)?\nANS=",a*a);
scanf("%s",&ans);//use %s in place of %c
}
return 0;
}
Use getchar() instead, after scanf
One that always works on Linux:
#include <termios.h>
#include <unistd.h>
void clean_stdin()
{
int stdin_copy = dup(STDIN_FILENO);
/* remove garbage from stdin */
tcdrain(stdin_copy);
tcflush(stdin_copy, TCIFLUSH);
close(stdin_copy);
}
You can use tcdrain and tcflush not only for in/out/err fd.