title: How to clean the buffer in c?
body: <p>I'm doing a lot of iterations in order to simulate the <code>bash</code> command (Homework) . </p>
  
  <p>The code works great , but the problem is that after a few iterations of inputs , the program 
  starts to have some buffer problems . I suspect it has something to do with all the <code>\n</code> of <code>Enter</code> hits . </p>
  
  <p>For example , the code starts like this : </p>
  
  <pre><code>#define BUFFER 4096
  #define RUN_FOREVER 1
  #define ERROR_SIGN -1
  #define TRUE 1
  #define FALSE 0
  
  int main(int argc , char * argv[] )
  {
      char input[BUFFER];
  
      //Get always a command line from the user.
      while(RUN_FOREVER)
  
      {
  
          if (isatty(0))
          {
              // input is from terminal
              // need to put something here 
          }
  
          char **separatedFormAmpersand ;
  
          int ampersandsCtr = 0, k=0,r=0;
          char *stringBeforeAmpersand = NULL;
  
          printf("$ ");
  
  
          memset(input, '\0', BUFFER);
  
           char ch;
  
          scanf("%[^\n]",input);
          scanf("%c",&amp;ch);
  
          if(0 == strcmp(input, "exit"))
              break;
  
          //Separate the command according to the "&amp;".
          stringBeforeAmpersand = strtok( input, "&amp;");
  ... // more code (quite a lot , frankly) 
  </code></pre>
  
  <p>Now , if the user hits the following inputs : </p>
  
  <pre><code>ls Debug/ | grep r
  ls &gt;&gt; file.jer &amp; ls &amp; ls &amp; ls
  ls &gt;&gt; file.jer
  </code></pre>
  
  <p>one after the other , then the code doesn't recognize the <code>ls</code> command when I hit input number <code>3</code> .</p>
  
  <p>If I enter each input in a single run of the code , everything works perfectly .</p>
  
  <p>Any ideas how to clean the buffer ? maybe <code>fflush</code> ? </p>
  
  <p>Thanks!</p>