why do i get this compiling error with the following code??

numbers.c: In function `main':
numbers.c:33: `for' loop initial declaration used outside C99 mode

int main()  {
   int oneNumber =0;            // hold the valid integer entered
   int countPositiveFactors =0; // accumulate a sum , so should be initialize to 0
   int sumPositiveFactors =0;    // accumulate a sum , so should be initialize to 0
   int prodPositiveFactors = 1; // accumulate a product so should be initialize to 1
  
   while (1)
   {
       printf("Enter an integer > 1 :");
       scanf("%d", &oneNumber);
       if (oneNumber > 1)
          break;
       else 
           printf("Error: Number should be > 1\n");
       _flushall(); // to flush all streams e.g. stdin 

   }
   
   printf("\n\nHere is a list of the positive factors of %d\n",oneNumber);
   for (int i=1;i<=oneNumber;i++)
   {
       if (oneNumber%i==0) // one factor found
       {
           countPositiveFactors++; // add 1 to count of positive factors
           printf("%d ",i);        // print this factor
           sumPositiveFactors+=i;  // add this factor e.g. i to the sum
           prodPositiveFactors*=i; //  multiply this factor with the previous factors

       }
   }
   
   printf ("\n\nThe number of positive factors of %d is %d.\n",oneNumber, countPositiveFactors);
   printf ("The sum of the positive factors of %d is %d.\n",oneNumber, sumPositiveFactors);
   printf ("The product of the positive factors of %d is %d.\n",oneNumber, prodPositiveFactors);
   
    return 0;
}

Recommended Answers

All 2 Replies

You're either using a C compiler or you're compiling under C mode in a C++ compiler. The problem is that you can't declare a variable in a for() under C.

Replace

for (int i=1;i<=oneNumber;i++)
   {
       if (oneNumber%i==0) // one factor found
       {
           countPositiveFactors++; // add 1 to count of positive factors
           printf("%d ",i);        // print this factor
           sumPositiveFactors+=i;  // add this factor e.g. i to the sum
           prodPositiveFactors*=i; //  multiply this factor with the previous factors

       }
   }

with this:

int i ;
for (i=1;i<=oneNumber;i++)
   {
       if (oneNumber%i==0) // one factor found
       {
           countPositiveFactors++; // add 1 to count of positive factors
           printf("%d ",i);        // print this factor
           sumPositiveFactors+=i;  // add this factor e.g. i to the sum
           prodPositiveFactors*=i; //  multiply this factor with the previous factors

       }
   }

And it should work.

You're either using a C compiler or you're compiling under C mode in a C++ compiler. The problem is that you can't declare a variable in a for() under C.

This used to be true, but since the C standard was revised in 1999 you can now declare a variable in a for loop, just like you can in C++. Consider upgrading your C compiler to something more recent. Or , as the warning suggests, do whatever your compiler requires to use 'C99' mode.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.