title: How to properly flush stdin in fgets loop
body: <p>I have found the example of clearing stdin using <code>while((c = getchar()) != '\n' &amp;&amp; c != EOF)</code> on here a few times, and tried to use it in a loop that gets input via fgets. I need to flush, since the loop takes the \n character from the last input and runs with it again.</p>
  
  <p>So what happens is that I have to press enter twice now. Why is this happening, and how can I fix this?</p>
  
  <pre><code>#define BUFFER_LIMIT 50
  do
  {
    int c;
    while ((c = getchar()) != '\n' &amp;&amp; c != EOF);
  
    printf("console&gt; ");
    fgets(input_buffer, BUFFER_LIMIT-1, stdin);
  
    if(do_something(input_buffer))
      break;
  } while(strncmp(input_buffer, "quit", 4) != 0);
  </code></pre>