943,525 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 10584
  • C RSS
Oct 9th, 2003
0

compiling error wtf!?!?

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stehigs321 is offline Offline
4 posts
since Oct 2003
Oct 10th, 2003
0

Re: compiling error wtf!?!?

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.
Reputation Points: 13
Solved Threads: 0
Light Poster
Dante Shamest is offline Offline
46 posts
since Apr 2003
Oct 11th, 2003
0

Re: compiling error wtf!?!?

Quote 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.
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: struct error (!?)
Next Thread in C Forum Timeline: hm.. wiered..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC