title: C while loop infinite loop EOF not working
body: <p>I am having problem with <code>EOF</code> in my while loop. It does not seem to simply end when <code>EOF</code> is entered but rather does this...</p>
  
  <p>How can I fix it and have the while loop stop and move on. Thanks.</p>
  
  <pre><code>    #define _CRT_SECURE_NO_WARNINGS
      #include &lt;stdio.h&gt;
      #include &lt;limits.h&gt;
  
      void findLargestandSmallest(int integer, int* largest, int* smallest, int* average, int count);
  
      int main(void)
      {
          //Local Declaration
          int integer;
          int largest;
          int smallest;
          int average;
          int count; // number count
  
          //Starting statemnets
          smallest = INT_MAX;
          largest = INT_MIN;
          count = 0;
  
          // Starting prompts
          printf("\nHello this program will take in intagers and print");
          printf("\nout the largest, smallest and avarage of integers");
          printf("\nenterd int.");
          printf("\nPlease enter in a integer ");
  
          while (scanf("%d", &amp;integer) != EOF)
          {
              if (integer != EOF)
              {
                  count++;
                  findLargestandSmallest(integer, &amp;largest, &amp;smallest, &amp;average, count);
              }
              else
              {
                  printf("\n \n");
              }
          }
  
          printf("\nThe largest number entered is %d and the smallest", largest);
          printf("\nwas %d and the average of all the numbers is %d\n", smallest, average);
          return 0;
      }
  
      void findLargestandSmallest(int integer, int *largest, int *smallest, int *average, int count)
      {
          int x; // just a holder variable 
  
          // finds average
          x = 0;
          x += integer;
          *average = (x / count);
  
          // Finds smallest and largest
          if (integer &lt;= *smallest)
          {
              *smallest = integer;
          }
          if (integer &gt;= *largest)
          {
              *largest = integer;
          }
          printf("Enter another integer or &lt;EOF&gt; to quit ");
          return;
      }
  
  
    [1]: http://i.stack.imgur.com/P0307.png
  </code></pre>
  
  <p>UPDATE: I found out what I was doing wrong. Its simple. In the while loop <code>while(scanf("%d", &amp;integer) != EOF)</code> don't set it like that but like this <code>(scanf("%d", &amp;integer))</code> EOF is understood. To simply call it in DOS use use "Ctrl+Z" on your last input. i.e "number^Z" is how it will look after using "Ctrl+Z" Also here is the better and working code for this problem for anyone else that runs into this.</p>
  
  <pre><code>#define _CRT_SECURE_NO_WARNINGS
  #include &lt;stdio.h&gt;
  #include &lt;limits.h&gt;
  
  void findLargestandSmallest(int integer, int* largest, int* smallest);
  
  int main(void)
  {
      //Local Declaration
      int integer;
      int largest;
      int smallest;
      int average;
      int sum;
      int count;
  
      //Starting statemnets
      smallest = INT_MAX;
      largest = INT_MIN;
      count = 0;
      sum = 0;
  
      // Starting prompts
      printf("\n--------------------------------------------------------");
      printf("\n-  Hello, this program will take in intagers and print -");
      printf("\n-  out the largest, smallest, and avarage  of the      -");
      printf("\n-  integers enterd.                                    -");
      printf("\n-  NOTE: To quit: use \"Ctrl+Z\" on the last integer     -");
      printf("\n-  you enter i.e \"number^z\"                        -");
      printf("\n--------------------------------------------------------\n");
      printf("\nEnter integers\n");
  
      // Finds largest and smallest number
      while (scanf("%d", &amp;integer))
      {
          sum += integer;
          count++;
          findLargestandSmallest(integer, &amp;largest, &amp;smallest);
      }
  
      // Finds average
      count--;
      average = (sum / count);
  
      // End prompts
      printf("\n--------------------------------------------------------");
      printf("\nThe largest number entered was %d, the smallest", largest);
      printf("\nwas %d, and the average of all the numbers is %d.", smallest, average);
      printf("\n--------------------------------------------------------");
      printf("\nGoodbye\n");
      return 0;
  }
  
  void findLargestandSmallest(int integer, int *largest, int *smallest)
  {
      if (integer &lt; *smallest)
      {
          *smallest = integer;
      }
      if (integer &gt; *largest)
      {
          *largest = integer;
      }
      return;
  }
  </code></pre>