compiling error wtf!?!?

Reply

Join Date: Oct 2003
Posts: 4
Reputation: stehigs321 is an unknown quantity at this point 
Solved Threads: 0
stehigs321 stehigs321 is offline Offline
Newbie Poster

compiling error wtf!?!?

 
0
  #1
Oct 9th, 2003
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

  1. int main() {
  2. int oneNumber =0; // hold the valid integer entered
  3. int countPositiveFactors =0; // accumulate a sum , so should be initialize to 0
  4. int sumPositiveFactors =0; // accumulate a sum , so should be initialize to 0
  5. int prodPositiveFactors = 1; // accumulate a product so should be initialize to 1
  6.  
  7. while (1)
  8. {
  9. printf("Enter an integer > 1 :");
  10. scanf("%d", &oneNumber);
  11. if (oneNumber > 1)
  12. break;
  13. else
  14. printf("Error: Number should be > 1\n");
  15. _flushall(); // to flush all streams e.g. stdin
  16.  
  17. }
  18.  
  19. printf("\n\nHere is a list of the positive factors of %d\n",oneNumber);
  20. for (int i=1;i<=oneNumber;i++)
  21. {
  22. if (oneNumber%i==0) // one factor found
  23. {
  24. countPositiveFactors++; // add 1 to count of positive factors
  25. printf("%d ",i); // print this factor
  26. sumPositiveFactors+=i; // add this factor e.g. i to the sum
  27. prodPositiveFactors*=i; // multiply this factor with the previous factors
  28.  
  29. }
  30. }
  31.  
  32. printf ("\n\nThe number of positive factors of %d is %d.\n",oneNumber, countPositiveFactors);
  33. printf ("The sum of the positive factors of %d is %d.\n",oneNumber, sumPositiveFactors);
  34. printf ("The product of the positive factors of %d is %d.\n",oneNumber, prodPositiveFactors);
  35.  
  36. return 0;
  37. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2003
Posts: 46
Reputation: Dante Shamest is an unknown quantity at this point 
Solved Threads: 0
Dante Shamest's Avatar
Dante Shamest Dante Shamest is offline Offline
Light Poster

Re: compiling error wtf!?!?

 
0
  #2
Oct 10th, 2003
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
  1. for (int i=1;i<=oneNumber;i++)
  2. {
  3. if (oneNumber%i==0) // one factor found
  4. {
  5. countPositiveFactors++; // add 1 to count of positive factors
  6. printf("%d ",i); // print this factor
  7. sumPositiveFactors+=i; // add this factor e.g. i to the sum
  8. prodPositiveFactors*=i; // multiply this factor with the previous factors
  9.  
  10. }
  11. }

with this:

  1. int i ;
  2. for (i=1;i<=oneNumber;i++)
  3. {
  4. if (oneNumber%i==0) // one factor found
  5. {
  6. countPositiveFactors++; // add 1 to count of positive factors
  7. printf("%d ",i); // print this factor
  8. sumPositiveFactors+=i; // add this factor e.g. i to the sum
  9. prodPositiveFactors*=i; // multiply this factor with the previous factors
  10.  
  11. }
  12. }

And it should work.
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 129
Reputation: Bob is an unknown quantity at this point 
Solved Threads: 1
Team Colleague
Bob Bob is offline Offline
Team Member

Re: compiling error wtf!?!?

 
0
  #3
Oct 11th, 2003
Originally Posted by Dante Shamest
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC