Page 1 of 1

NXC "File Error!"

Posted: 12 Sep 2012, 22:44
by jld001

Code: Select all

task main() {
int select=10, board[9], i;
for (i=0; i<9; i++)
    {
    board[i]=0;
    }
PlayTone(500,500);
Wait(1000);
for (i=9; i>0; i--) { if (board[i]==0) {PlayTone(1000,500);Wait(1000);} } //ERROR HERE(?)
PlayTone(2000,500);
Wait(1000);
}
Will someone try out this NXC code? My NXT with 1.31 Enhanced Firmware is says "File Error! -1"
On the standard NXT firmware it just says "File Error!"
I think that the issue is something to do with arrays. Any ideas? Thanks!

Re: NXC "File Error!"

Posted: 12 Sep 2012, 23:57
by mattallen37
You are trying to access board[9] (tenth element), when you declared it with only 9 elements (numbered 0-8). The second for loop repeats 9 times, with variable i's value starting at 9 and going down to 1 (when i gets to 0, it breaks to the end of the for loop).

I think this is what you want:

Code: Select all

task main() {
  int select=10, board[9], i;
  for (i=0; i<9; i++)
  {
    board[i]=0;
  }
  PlayTone(500,500);
  Wait(1000);
  for (i=8; i>(-1); i--){
    if(board[i] == 0){
      PlayTone(1000,500);
      Wait(1000);
    }
  }
  PlayTone(2000,500);
  Wait(1000);
}
With this code, the value of i in the second for loop starts at 8 and goes down to 0. When it becomes -1, the program jumps to the end of the for loop.

Re: NXC "File Error!"

Posted: 12 Sep 2012, 23:58
by mattallen37
In NXC, trying to access an element beyond what has been declared will crash the program (and IIRC, with "File Error! -1").

Re: NXC "File Error!"

Posted: 13 Sep 2012, 02:39
by jld001
Thanks, those off-by-one errors are annoying.