title: how to exit while loop in C?
body: <pre><code>#include&lt;stdio.h&gt;
  #define LINESIZE 1024
  
  int n, sum =0;
  char line[LINESIZE];
  
  int main() {
      while(1) {
          printf("enter an integer: ");
          if(!fgets(line, LINESIZE, stdin)) {
              clearerr(stdin);
              break;
          }
  
          if (sscanf(line, "%d", &amp;n) == 1)
              sum += n;
      }
      printf("%d \n",sum);
  }
  </code></pre>
  
  <p>When I run this in Cygwin, the output seems infinite and I don't know how to return <code>sum</code>? Am I missing something?</p>
  
  <pre><code>enter an integer: 1
  enter an integer: 2
  enter an integer: 3
  enter an integer: 4
  enter an integer: 5
  enter an integer: 6
  </code></pre>