mercredi 31 janvier 2018

If statement isn't being caught

I can't for the life of me figure out why C is ignoring my if statement.

I'm trying to skip all the procedures in the while statement when the input is -1000 (so that it doesn't print before exiting the program). Here is my code:

int main()
{
  int count = 1;
  int grade1;
  int grade2;
  double sum;
  double average;

  printf("Please input a number of grades: \n");

  scanf("%d", &grade1);
  printf("Sum is: %d.000000 \n", grade1);
  printf("Average is: %d.000000 \n", grade1);
  count++;

  sum = grade1;

  while(grade2 != -1000) 
  {
    if(grade2 != -1000)
    {
      scanf("%d", &grade2);

      sum = sum + grade2;
      average = sum / count;

      printf("Sum is: %lf \n", sum);
      printf("Average is: %lf \n", average);

      grade1 = sum; //Converting the sum back into an int
      count++;
    }
  }
  return 0;
}

enter image description here

Here is a link to an image of my output. As you can see, even when grade2 is given -1000, the if statement is ignored, and another 2 lines are printed to the screen before the program exits. How can I fix this? Is this some sort of oddity of how C works?

Aucun commentaire:

Enregistrer un commentaire