title: ProbIem with EOF in C
body: <p>I'm writing a program which is supposed to read two strings that can contain line breaks and various other characters. Therefore, I'm using EOF (Ctrl-Z or Ctrl-D) to end the string.</p>
  
  <p>This works fine with the first variable, but with the second variable, however, this seems to be problematic as apparently something is stuck in the input buffer and the user doesn't get to type in anything.</p>
  
  <p>I tried to clean the buffer with <code>while (getchar() != '\n');</code> and several similar variations but nothing seems to help. All cleaning attempts have resulted in an infinite loop, and without cleaning, adding the second variable is impossible.</p>
  
  <p>The characters for both of the variables are read in a loop like this: <code>while((c = getchar()) != EOF)</code>, which would suggest it is EOF what I have stuck in my buffer. Or does it affect the behavior of the program in some other way? Is there something wrong with the logic I'm using?</p>
  
  <p>I'm starting to get bit desperate after struggling with this for hours.</p>
  
  <p>code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main(void)
  {
      int x = 0;
      int c;
      char a[100];
      char b[100];
  
      printf("Enter a: ");
      while((c = getchar()) != EOF)
      {
          a[x] = c;
          x++;
      }
      a[x] = '\0';
      x = 0;
  
      /*while (getchar() != '\n'); - the non-working loop*/
  
      printf("\nEnter b: ");
      while((c = getchar()) != EOF)
      {
          b[x] = c;
          x++;
      }
      b[x] = '\0';
  
      printf("\n\nResults:\na: %s\n", a);
      printf("b: %s\n", b);
  
      return(0);
  }
  </code></pre>