for loop error

Reply

Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

for loop error

 
0
  #1
Jun 13th, 2005
i have this for loop in my program that i am creating but it is counting further than it is supposed to and trying to call to an unknown variable thus creating an error. here is the code that kicks back the error.

  1. for (i = 1; i <= me.spellcasterlevel; i++){
  2. if (me.firelevel >= i){
  3. //cout << i << ": " << me.fire[i].name << endl;
  4. }
  5. }
  6. cin >> choice;
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: for loop error

 
0
  #2
Jun 13th, 2005
Arrays are indexed from 0 to N-1, not 1 to N. The usual idiom is like this.
for ( i = 0; i < N; ++i )
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

Re: for loop error

 
0
  #3
Jun 14th, 2005
that is basically what i did, (p.s. this is in c++ incase u were wondering) i declared a starting point, i=1, declared the stoping point, when it hits me.spellcasterlevel, then i said what to do (go up), i++. at least that is what i thought it is.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: for loop error

 
0
  #4
Jun 14th, 2005
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int i, array[5] = {10,20,30,40,50};
  6. puts("right");
  7. for (i = 0; i < 5; ++i)
  8. {
  9. printf("array[%d] = %d\n", i, array[i]);
  10. }
  11. puts("wrong");
  12. for (i = 1; i <= 5; ++i)
  13. {
  14. printf("array[%d] = %d\n", i, array[i]);
  15. }
  16. return 0;
  17. }
  18.  
  19. /* my output
  20. right
  21. array[0] = 10
  22. array[1] = 20
  23. array[2] = 30
  24. array[3] = 40
  25. array[4] = 50
  26. wrong
  27. array[1] = 20
  28. array[2] = 30
  29. array[3] = 40
  30. array[4] = 50
  31. array[5] = 1245112
  32. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC