Pages: [1]   Go Down
Author Topic: How to use an int to pass variable0 variable1 etc  (Read 507 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,

I have implemented a dynamic menu system on utft/touch screen, which works fine, but I am sure there must be a more efficient way of doing things. I am new to C++ and have zero grasp of classes so far, so go gentle with me please.... smiley-red

The specific part I am asking about involves the following code snippet.
Code:
if (zz==1) myButtons.drawButton(but1);
if (zz==2) myButtons.drawButton(but2);
if (zz==3) myButtons.drawButton(but3);
if (zz==4) myButtons.drawButton(but4);
if (zz==5) myButtons.drawButton(but5);
if (zz==6) myButtons.drawButton(but6);
if (zz==7) myButtons.drawButton(but7);
if (zz==8) myButtons.drawButton(but8);
if (zz==9) myButtons.drawButton(but9);

What I would like to do is something like......
Code:
if(zz>0 && zz<=9)
{   
avar="but"+zz;
myButtons.drawButton(avar);
}

Furthermore, if anyone is feeling generous/helpful, and could show me how to optimize the full routine so I might learn something, that would be VERY much appreciated!!

The relevant section of code is thus ..

Code:
  {
    int butx=myButtons.addButton(  8, 223, 16, 16, "X"); // None
    int but4=myButtons.addButton(128, 223, 16, 16, "4"); // 4 , 14 , 24 etc
    int but5=myButtons.addButton(152, 223, 16, 16, "5"); // 5 , 15 , 25 etc
    int but6=myButtons.addButton(176, 223, 16, 16, "6"); // 6 , 16 , 26 etc
    int but7=myButtons.addButton(200, 223, 16, 16, "7"); // 7 , 17 , 27 etc
    int but8=myButtons.addButton(224, 223, 16, 16, "8"); // 8 , 18 , 28 etc
    int but9=myButtons.addButton(248, 223, 16, 16, "9"); // 9 , 19 , 29 etc
    int butN=myButtons.addButton(272, 223, 16, 16, ">"); // +10
    typedef struct{
      char desc[27];                      /* Route description     */
      char fname[19];                     /* Route filename        */
      char startLat[13];                  /* Start Lat             */
      char startLon[13];                  /* Start Lon             */
      char destLat[13];                   /* Dest Lat              */
      char destLon[13];                   /* Dest Lon              */
    }
    ROUTES;
    int routecount=0;
    ROUTES routes[50];
    ifstream indata;
    indata.open("route/index.dat");
    int result=0;
    unsigned long posx;
    while(!result)
    {
      indata.get(routes[routecount].desc, sizeof(routes[routecount].desc),',');          // read Route Description
      indata.get();
      indata.get(routes[routecount].fname, sizeof(routes[routecount].fname),',');        // read Route Filename
      indata.get();
      indata.get(routes[routecount].startLat, sizeof(routes[routecount].startLat),',');  // read Route Start Latitude
      indata.get();
      indata.get(routes[routecount].startLon, sizeof(routes[routecount].startLon),',');  // read Route Start Longitude
      indata.get();
      indata.get(routes[routecount].destLat, sizeof(routes[routecount].destLat),',');    // read Route End Latitude
      indata.get();
      indata.get(routes[routecount].destLon, sizeof(routes[routecount].destLon));        // read Route End Longitude
      indata.get();
      //////////////////////////////////////////////
      // Deal with stupid Windows file EOF issues...
      posx=indata.tellg();
      indata.get(NULL,10);
      result=1;
      if(indata.gcount()>=3) {
        result=0;
        indata.seekg(posx);
      }
      //////////////////////////////////////////////
      if(routecount<50)
        routecount++;
    }
    indata.close();
    myprintf("Routes found %d\n", routecount);
    /*******************************************************************
    /*                 Dynamic Menu                                    *
    /******************************************************************/
    int zzpage=0, prev_zzpage=0;
    int zz=0;
    pressed_button =-1;
    result=-1;
    bool menu_drawn=false;
    myGLCD.clrScr();
    myGLCD.print("Select Route", 0, 0);
    myGLCD.setFont(SmallFont);
    myGLCD.print(" X)None", 0, 32);
    myButtons.drawButton(butx);
    while(pressed_button ==-1 || result!=-1)
    {
      //      myprintf("Buttons loop, pressed_button=%d, result=%d\n", pressed_button, result);
      while((zz+zzpage)<routecount && menu_drawn==false)
      {
        //        myprintf("draw menu loop, zz=%d, zzpage=%d, prev_zzpage=%d\n", zz, zzpage, prev_zzpage);
        if(zzpage==prev_zzpage)
        {
          if(zz+zzpage<routecount)
          {
            if (zz==0)
            {
              myGLCD.setColor(0,0,0);
              myGLCD.fillRect(0,48,290,208);
              myGLCD.fillRect(32,223,290,239);
              myGLCD.setColor(255,255,0);
              myButtons.drawButton(but0);
            }
            if (zz==1) myButtons.drawButton(but1);
            if (zz==2) myButtons.drawButton(but2);
            if (zz==3) myButtons.drawButton(but3);
            if (zz==4) myButtons.drawButton(but4);
            if (zz==5) myButtons.drawButton(but5);
            if (zz==6) myButtons.drawButton(but6);
            if (zz==7) myButtons.drawButton(but7);
            if (zz==8) myButtons.drawButton(but8);
            if (zz==9) myButtons.drawButton(but9);
            if (zz==10) {
              myButtons.drawButton(butN);
              zzpage+=10;
              zz=0;
              break;
            }
            myGLCD.printNumI(zz+zzpage, 0, ((zz*16)+48),2);
            myGLCD.print(")" , 16, ((zz*16)+48));
            myGLCD.print(routes[zz+zzpage].desc, 24, ((zz*16)+48));
            zz++;
          }
        }
      }
      menu_drawn=true;
      pressed_button = myButtons.checkButtons();
      if(pressed_button==butN) {
        prev_zzpage= zzpage;
        menu_drawn=false;
        pressed_button=-1;
      }
      if(pressed_button==but0) {
        result=prev_zzpage;
        break;
      }
      if(pressed_button==but1) {
        result=1+prev_zzpage;
        break;
      }
      if(pressed_button==but2) {
        result=2+prev_zzpage;
        break;
      }
      if(pressed_button==but3) {
        result=3+prev_zzpage;
        break;
      }
      if(pressed_button==but4) {
        result=4+prev_zzpage;
        break;
      }
      if(pressed_button==but5) {
        result=5+prev_zzpage;
        break;
      }
      if(pressed_button==but6) {
        result=6+prev_zzpage;
        break;
      }
      if(pressed_button==but7) {
        result=7+prev_zzpage;
        break;
      }
      if(pressed_button==but8) {
        result=8+prev_zzpage;
        break;
      }
      if(pressed_button==but9) {
        result=9+prev_zzpage;
        break;
      }
      if(pressed_button==butx) {
        result=100;
        break;
      }
    }
    /********************************************************************************/
    /*  result = between 0 and 'routecount' for valid choice or 100 = 'none' choice */
    /********************************************************************************/
    if(result<100)
    {
      myprintf("Select route button\n");
      myprintf("Selected route %d\n", result);
      myprintf("Route selected %s\n",routes[result].desc);
      myprintf("Filename of selected route %s\n",routes[result].fname);
      myprintf("Start lat of route selected %s\n",routes[result].startLat);
      myprintf("Start lon of route selected %s\n",routes[result].startLon);
      myprintf("End lat of route selected %s\n",routes[result].destLat);
      myprintf("End lon of route selected %s\n",routes[result].destLon);
      strcpy(routefile , routes[result].fname);
      Home_LAT = atof(routes[result].destLat);
      Home_LON = atof(routes[result].destLon);
      drawroute=true;
      //    do 'something' with startLat, startLon ?
    }
    else
    {
      // none selected
      strcpy(routefile, "");
      Home_LAT = 0;
      Home_LON = 0;
      drawroute=false;
    }
    myButtons.deleteButton(butx);
    myButtons.deleteButton(but4);
    myButtons.deleteButton(but5);
    myButtons.deleteButton(but6);
    myButtons.deleteButton(but7);
    myButtons.deleteButton(but8);
    myButtons.deleteButton(but9);
    myButtons.deleteButton(butN);
  }

Thanks for any help! Regards,

Graham
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wouldn't an array do the trick?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wouldn't an array do the trick?

I guess sometimes we just need somebody to state the bleeding obvious.... Thankyou!!!

Yes it did, and the code has shrunk quite substantially.

Code:
 char symb[]="0";
  for(int buts=0; buts<4; buts++)
  {
    but[buts]=myButtons.addButton(((24*buts)+32), 223 , 16 ,16 , symb+buts ) ;
    myButtons.drawButton(but[buts]);
  }

EDIT:
char symb[]="0";
but[buts]=myButtons.addButton(((24*buts)+32), 223 , 16 ,16 , symb+buts ) ; 

Did not work.... All button symbols were '9'  any ideas?

What I had to do was :

char symb[][10] ={ "0","1","2", ....."9"};
but[buts]=myButtons.addButton(((24*buts)+32), 223 , 16 ,16 , symb[buts] ) ; 


Replaced.

Code:
  //but0=myButtons.addButton( 32, 223, 16, 16, "0"); // 0 , 10 , 20 etc
  //but1=myButtons.addButton( 56, 223, 16, 16, "1"); // 1 , 11 , 21 etc
  //but2=myButtons.addButton( 80, 223, 16, 16, "2"); // 2 , 12 , 22 etc
  //but3=myButtons.addButton(104, 223, 16, 16, "3"); // 3 , 13 , 23 etc
  //myButtons.drawButton(but0);
  //myButtons.drawButton(but1);
  //myButtons.drawButton(but2);
  //myButtons.drawButton(but3);

Pity I didn't recognise my own psuedo code to be an array!!  smiley-roll

Regards,

Graham

PS I have attached a couple of piccies of the result if anyone wants to see?


* DSC01435.JPG (178.77 KB, 1021x766 - viewed 33 times.)

* DSC01434.JPG (176.64 KB, 1021x766 - viewed 29 times.)
« Last Edit: September 20, 2013, 04:51:20 pm by ghlawrence2000 » Logged

Offline Offline
God Member
*****
Karma: 19
Posts: 785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anytime you find yourself putting numbers on the end of variable names you should immediately think, "I might need an array."
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50039
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Anytime you find yourself putting numbers on the end of variable names you should immediately think, "I might need an array."
   
I fixed that for you.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8583
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char symb[]="0";
 ... symb+buts

You only have a single (well 2 really but let's forget the terminator) byte in the array, you can't index past location 0.

What data type is the last parm to addButton()? You are treating it as though a string is required, is that the case? If so is that the string that becomes the button label?

If you just want an ASCII number then

Code:
'0' + buts

Should work.

_____
Rob

Logged

Rob Gray aka the GRAYnomad www.robgray.com

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, the last parameter is an ascii character string , implicitly declared such as "a", "Y", "0" ,"Yes" etc.....however.... '0'+buts gave something like......ambiguous overloaded function ..... yada yada.... hence the explicit array definition.....??? The only way I could resolve it was declare an entire array.... Any help greatly appreciated...

Regards,

G

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50039
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Yes, the last parameter is an ascii character string , implicitly declared such as "a", "Y", "0" ,"Yes" etc.....however.... '0'+buts gave something like......ambiguous overloaded function ..... yada yada.... hence the explicit array definition.....??? The only way I could resolve it was declare an entire array.... Any help greatly appreciated...
I'm nearly certain that "yada yada" does not appear in any of the messages that the GNU compiler can generate.

Posting SPECIFIC code that generates an error message, and the EXACT error message, if you don't understand it, is going to be necessary. It's the only way we can explain why what you are doing is wrong, in more detail that the compiler message, and suggest how to do what you are trying to do, in a way that the compiler will be a happy camper AND the code will work properly. (Note that the compiler accepting the code and it working properly are not the same thing.)
Logged

Pages: [1]   Go Up
Jump to: