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.

for (i = 1; i <= me.spellcasterlevel; i++){
                    if (me.firelevel >= i){
                            //cout << i << ": " << me.fire[i].name << endl;
                    }
                }
                cin >> choice;

Recommended Answers

All 3 Replies

Arrays are indexed from 0 to N-1, not 1 to N. The usual idiom is like this.

for ( i = 0; i < N; ++i )

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.

#include <stdio.h>

int main()
{
   int i, array[5] = {10,20,30,40,50};
   puts("right");
   for (i = 0; i < 5; ++i)
   {
      printf("array[%d] = %d\n", i, array[i]);
   }
   puts("wrong");
   for (i = 1; i <= 5; ++i)
   {
      printf("array[%d] = %d\n", i, array[i]);
   }
   return 0;
}

/* my output
right
array[0] = 10
array[1] = 20
array[2] = 30
array[3] = 40
array[4] = 50
wrong
array[1] = 20
array[2] = 30
array[3] = 40
array[4] = 50
array[5] = 1245112
*/
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.