NXT 2.0 Hanging in this function...

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
monxc
Posts: 18
Joined: 17 Oct 2011, 10:23

NXT 2.0 Hanging in this function...

Post by monxc »

Hello,

can somebody shed some light on this for me, please? I've done as much banging my head against it as I can - I imagine it's something quite straight forward, embarrassingly...

Code: Select all

string optMenu(string strPrompt, string strOptions)
{
  int intIndex = 0;
  int intOptions = 0;
  int intOptStart = 0;
  int intOptEnd = 0;
  int intLineT = 40;
  int intLineS = 40;
  int intSelected = 0;
  int strArrOptions[5];      //never more than five options, so set it here rather than try to ArrayInit it etc...

  //create the options array from the string passed in
/*  while(intIndex < StrLen(strOptions)){
    if(SubStr(strOptions,intIndex,1) == ","){
      intOptEnd = intIndex - 1;                                                              //define the end of this option
      strArrOptions[intOptions] = SubStr(strOptions,intOptStart,intOptEnd - intOptStart);
      intOptStart = intIndex + 1;                                                            //define the start of the next option (if there is one)
      intOptions++;                                                                          //increment the number of options found
    }
    intIndex++;                                                                              //increment the string pointer
  }

  //add the last option (will be missed by the above loop, as no comma is encountered
  strArrOptions[intOptions] = SubStr(strOptions,intOptStart,StrLen(strOptions) - intOptStart;
  intOptions++;
 */
 
  strArrOptions[0] = "Turn";
  strArrOptions[1] = "Move";
  strArrOptions[2] = "Set WPT";
  strArrOptions[3] = "Options...";
  strArrOptions[4] = "";

  intOptions = 4;
 
  while(!ButtonPressed(BTN4,FALSE)){
    TextOut(0,56,strPrompt/*,DRAW_OPT_CLEAR_WHOLE_SCREEN*/);
    for(intIndex = 0; intIndex < intOptions; intIndex++){
      TextOut(8,intLineT,">" + strArrOptions[intIndex]);
      intLineT -= 8;
    }
    intLineT = 40;
  }
  return strArrOptions[intSelected];
}
Sorry for the big, messy function, but somewhere in here, although it complies fine, it causes my NXT 2.0 to hang (batteries out/batteries in again to fix it!)

What I'm trying to do is this - I want a general purpose function that will put a menu of text options on the NXT LCD screen. Because I can't seem to pass an array to a function (I keep getting the error "Math Factor expected"), I'm instead passing a string and then parsing it to build an array inside the function itself. As you can see, however, all the string parsing code is commented out, because it doesn't work... I can't see what's wrong with it, but the array it creates is beyond 5 elements in size (how?!) and always contains only the last text option in every element... Anyway, whilst trying to debug this conundrum, I decided to hard-code the array (strArrOptions[]) with the options I'd be passing in as strOptions. The result was just strPrompt on the screen, as I'd forgotten to set intOptions to be greater than 0; once I did this, however, although the code happily compiles and uploads to the NXT, when I run it, the NXT freezes.

I imagine I've put it into some sort of non-ending loop, but I can't see why :(

Any thoughts on this would be gratefully received,

Thanks,

monxc_
---111 040 150 141 166 145 040 156 157 040 151 144 145 141 040 167 150 141 164 040 111 047 155 040 144 157 151 156 147 056 056 056---
monxc
Posts: 18
Joined: 17 Oct 2011, 10:23

Re: NXT 2.0 Hanging in this function...

Post by monxc »

...er...well, I said it'd be something simple - I'd declared the array (strArrOptions[]) that was to hold the string options as an integer...oops...I'd even named it with str at the front!!!

[puffs out cheeks] I think my response to that is spherical and in the plural...clearly time to quit coding for the day :roll:

monxc_
---111 040 150 141 166 145 040 156 157 040 151 144 145 141 040 167 150 141 164 040 111 047 155 040 144 157 151 156 147 056 056 056---
Post Reply

Who is online

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