title: how not to get an extra count in a while loop?
body: <p>I have the following code and every time i run it, I get an extra count. Say for example that I input 8 first round and then a 2 next round and exit with the sentinel -1, the sum will be 10 as expected, but the count will be 3. I've debugged the program and no matter if count comes before the scanf() or after, I still get a 3 value. One possible solution is to initialized count to -1. However, I feel I shouldn't have to and setting count equal to zero should work. Do I have to set count to -1?</p>
  <pre><code>#include &lt;stdio.h&gt;
  
  void calculateAverage()
  {
      int grade = 0, count, sum = 0;
      double average;
      
      count = 0;
      
      while(grade != -1)
      {
          
          sum += grade;
          count++;
  
          printf(&quot;input a grade: \n&quot;);
          scanf(&quot;%d&quot;, &amp;grade);
          
  
      }
      
      average = (double)(sum)/(double)(count);
      printf(&quot;%.2lf&quot;, average);
  
      return;
  }
  
  int main( )
  {
      while (1)
          calculateAverage();
  
      return 0;
  }
  </code></pre>