-1

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?

Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

5

When you do this the first time

  while(grade2 != -1000) 

the variable grade2 is uninitialized.

Consequently your code has undefined behavior

Make sure to initialize it like:

int grade2 = 0; // To zero or whatever you want

Further - always check the value returned by scanf. So instead of

scanf("%d", &grade1);

do

if (scanf("%d", &grade1) != 1)
{
    // Add error handling here
}

Your next problem is that you don't scan grade2 before checking whether it is -1000. Move the scan before the if-statement.

Maybe what you want to do is:

  int grade2 = 0;
  while(grade2 != -1000) 
  {
      if (scanf("%d", &grade2) != 1)
      {
          // Add error handling here
      }
      if(grade2 != -1000)
      {
         ... 

so that you scan for the first grade2 before you do the if(grade2 != -1000) and enters the calculation code

Written differently this could be:

  while(1) 
  {
      if (scanf("%d", &grade2) != 1)
      {
          // Add error handling here
      }
      if(grade2 == -1000) break;  // Terminate the while

      sum = sum + grade2;
      ....
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
2

While it's true that grade2 should be initialized and the return for scanf() should be checked, that's not the main problem the poster is running into. The problem is that he checks

if(grade2 != -1000)

AFTER he has already processed grade2. He should move

scanf("%d", &grade1);

before

if(grade2 != -1000)
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
0

The if statement in your while loop is redundant because, the loop will not iterate unless the condition that controls it is true, and the if statement comes directly after that, checking for the same condition, whilst grade2 is unchaged.

Instead, you need to move it to after the scanf() call because that will modify the variable grade2, and don't forget to initialize your variables before using them or else you'll have undefined behavior.

int main(void)
{

    //....
    int grade2 = 0; // initialized...
    //....

    while (grade2 != -1000)
    {
        scanf("%d", &grade2);
        if (grade2 != -1000)
        {
            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++;
        }
    }
}
machine_1
  • 4,266
  • 2
  • 21
  • 42