NXC "File Error!"

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
jld001
Posts: 19
Joined: 07 Sep 2012, 21:32

NXC "File Error!"

Post 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!
My Githubz of Source Codez, in order of awesomeness:
https://github.com/ArtskydJ/NXC_Source
https://github.com/ArtskydJ/NXC_Tests
My Lego Creations, in no particular order:
LEGO Bop-It - http://www.youtube.com/watch?v=6TsqrBVKbj0
LEGO Safe - http://www.youtube.com/watch?v=MyDRfVquHuY
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC "File Error!"

Post 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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC "File Error!"

Post by mattallen37 »

In NXC, trying to access an element beyond what has been declared will crash the program (and IIRC, with "File Error! -1").
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
jld001
Posts: 19
Joined: 07 Sep 2012, 21:32

Re: NXC "File Error!"

Post by jld001 »

Thanks, those off-by-one errors are annoying.
My Githubz of Source Codez, in order of awesomeness:
https://github.com/ArtskydJ/NXC_Source
https://github.com/ArtskydJ/NXC_Tests
My Lego Creations, in no particular order:
LEGO Bop-It - http://www.youtube.com/watch?v=6TsqrBVKbj0
LEGO Safe - http://www.youtube.com/watch?v=MyDRfVquHuY
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests