title: How can one flush input stream in C?
body: <p>I am not able to flush <em>stdin</em> here, is there a way to flush it? If not then how to make <code>getchar()</code> to take a character as input from user, instead of a "\n" left by <code>scanf()</code> in the input buffer??</p>
  
  <pre><code>#include "stdio.h"
  #include "stdlib.h"
  
  int main(int argc,char*argv[]) {
      FILE *fp;
      char another='y';
      struct emp {
          char name[40];
          int age;
          float bs;
      };
      struct emp e;
      if(argc!=2) {
          printf("please write 1 target file name\n");
      }
      fp=fopen(argv[1],"wb");
      if(fp==NULL) {
          puts("cannot open file");
          exit(1);
      }
      while(another=='y') {
          printf("\nEnter name,age and basic salary");
          scanf("%s %d %f",e.name,&amp;e.age,&amp;e.bs);
          fwrite(&amp;e,sizeof(e),1,fp);
  
          printf("Add another record (Y/N)");
          fflush(stdin);
          another=getchar();
      }
      fclose(fp);
      return 0;
  }
  </code></pre>
  
  <p>EDIT: updated code, still not working properly </p>
  
  <pre><code>#include "stdio.h"
  #include "stdlib.h"
  
  int main(int argc,char*argv[]) {
      FILE *fp;
      char another='y';
      struct emp {
          char name[40];
          int age;
          float bs;
      };
      struct emp e;
      unsigned int const BUF_SIZE = 1024;
      char buf[BUF_SIZE];
  
      if(argc!=2) {
          printf("please write 1 target file name\n");
      }
      fp=fopen(argv[1],"wb");
      if(fp==NULL) {
          puts("cannot open file");
          exit(1);
      }
      while(another=='y') {
          printf("\nEnter name,age and basic salary : ");
          fgets(buf, BUF_SIZE, stdin);
          sscanf(buf, "%s %d %f", e.name, &amp;e.age, &amp;e.bs);
          fwrite(&amp;e,sizeof(e),1,fp);
          printf("Add another record (Y/N)");
          another=getchar();
      }
      fclose(fp);
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>dev@dev-laptop:~/Documents/c++_prac/google_int_prac$ ./a.out emp.dat
  
  Enter name,age and basic salary : deovrat 45 23
  Add another record (Y/N)y
  
  Enter name,age and basic salary : Add another record (Y/N)y
  
  Enter name,age and basic salary : Add another record (Y/N)
  </code></pre>