Case statement works after shuffling lines

I am simply changing the position of case E_ELEM_BTN12, case E_ELEM_BTN13, case E_ELEM_BTN14, case E_ELEM_BTN15. Putting them at the beginning of the case tree works, as in the codes within them executes if a callback was initiated. But putting these cases at the end of the case tree doesn’t execute their respective codes when the touchscreen is pressed on the associated buttons (case statements).
the problematic case statements are not populated with any code now because they were not being called previously. So I put a simple command in cases of BTN12 and BTN 15 to switch back to page 1 of touchscreen to see if they are being called.
It is a project using the GUISlice library.

So here the case statements of the problematic aforementioned elements works:

if ( eTouch == GSLC_TOUCH_UP_IN ) {
    // From the element's ID we can determine which button was pressed.
    switch (pElem->nId) {
//<Button Enums !Start!>
      case E_ELEM_BTN12:
        gslc_SetPageCur(&m_gui, E_PG_MAIN);
        break;
      case E_ELEM_BTN13:
        break;
      case E_ELEM_BTN14:
        break;
      case E_ELEM_BTN15:
        gslc_SetPageCur(&m_gui, E_PG_MAIN);
        break;
      case E_ELEM_BTN1:
        timer = gslc_ElemXSpinnerGetCounter(&m_gui, &m_sXSpinner1);
        snprintf(acTxt,4,"%u",timer);
        gslc_ElemSetTxtStr(&m_gui,m_pElemSetTime,acTxt);
        
        //store the value from spinner into sd card test.txt in line 1
        DeleteLineFromFile("test.txt", 1);
        timerFile = SD.open("test.txt", FILE_WRITE);
        //Serial.println("Writing to test.txt...");
        timerFile.println(String(timer, DEC));
        //close the file:
        timerFile.close();
        //Serial.println("done.");
        break;
      case E_ELEM_BTN4:
      //continue from stopwatch
        
        play=1;
        DeleteLineFromFile("runstatus.txt", 1);
        runstatus = SD.open("runstatus.txt", FILE_WRITE);
        //Serial.println("Writing to runstatus.txt...");
        runstatus.println(String(play, DEC));
        //close the file:
        runstatus.close();
        //Serial.println("done.");   
        gslc_ElemXCheckboxSetState(&m_gui,m_ElemRadio1, true);
        gslc_ElemXCheckboxSetState(&m_gui,m_ElemRadio2, false);
        break;
      case E_ELEM_BTN5:
        play=0;
        DeleteLineFromFile("runstatus.txt", 1);
        runstatus = SD.open("runstatus.txt", FILE_WRITE);
        //Serial.println("Writing to runstatus.txt...");
        runstatus.println(String(play, DEC));
        //close the file:
        runstatus.close();
        //Serial.println("done."); 
        pause_value=stopwatch;
        gslc_ElemXCheckboxSetState(&m_gui,m_ElemRadio1, false);
        gslc_ElemXCheckboxSetState(&m_gui,m_ElemRadio2, true);
        break;
      case E_ELEM_BTN6:
        gslc_SetPageCur(&m_gui, E_PG2);
        break;
      case E_ELEM_BTN7:
        gslc_SetPageCur(&m_gui, E_PG3);
        break;
      case E_ELEM_BTN11:
        gslc_ElemXTextboxReset(&m_gui,m_pElemTextbox1);
        //concat_relay="\0";
        skip_relay = gslc_ElemXSpinnerGetCounter(&m_gui, &m_sXSpinner5);
        int indexofskippedrelay= skip_relay-1;
        //sprintf(skipped_relay[indexofskippedrelay], "%d", skip_relay);
        //skipped_relay[skip_relay-1]=String(skip_relay);
        if (skip_relay>0)
        {
          skipped_relay[indexofskippedrelay]= !skipped_relay[indexofskippedrelay];
          }
          Serial.print("boolean array is ");
        for (int i=0;i<18;i++)
        {
          Serial.print(skipped_relay[i]);
          }
          Serial.println("Done printing boolean");
          int x=0;
          int y=0;
          Serial.print("printing actxt2 ");
          while(1)
        { if (skipped_relay[y]==true)
          {
           acTxt2[x]='1';
          }
          else
          {
            acTxt2[x]='0';
            }
          Serial.print(acTxt2[x]);
          x++;
          y++;
          
          if (y==18)
          {break;}
          acTxt2[x]=',';
          x++;
          
          }
          Serial.println(" Done printing acTxt2");
        
        //snprintf(acTxt,MAX_STR,"%u",skip_relay);
        //Serial.println(skipped_relay);
        
        //snprintf(acTxt2,MAX_STR,"%s",*skipped_relay);
        //Serial.println("value of acTxt2 is "+acTxt2);
        //snprintf(acTxt,MAX_STR,concat_relay);
        gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,acTxt2);
        //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,*skipped_relay);
        break;
//<Button Enums !End!>
  default:
        break;
    }
  }

and here it doesn’t work

if ( eTouch == GSLC_TOUCH_UP_IN ) {
    // From the element's ID we can determine which button was pressed.
    switch (pElem->nId) {
//<Button Enums !Start!>
      case E_ELEM_BTN1:
        timer = gslc_ElemXSpinnerGetCounter(&m_gui, &m_sXSpinner1);
        snprintf(acTxt,4,"%u",timer);
        gslc_ElemSetTxtStr(&m_gui,m_pElemSetTime,acTxt);
        
        //store the value from spinner into sd card test.txt in line 1
        DeleteLineFromFile("test.txt", 1);
        timerFile = SD.open("test.txt", FILE_WRITE);
        //Serial.println("Writing to test.txt...");
        timerFile.println(String(timer, DEC));
        //close the file:
        timerFile.close();
        //Serial.println("done.");
        break;
      case E_ELEM_BTN4:
      //continue from stopwatch
        
        play=1;
        DeleteLineFromFile("runstatus.txt", 1);
        runstatus = SD.open("runstatus.txt", FILE_WRITE);
        //Serial.println("Writing to runstatus.txt...");
        runstatus.println(String(play, DEC));
        //close the file:
        runstatus.close();
        //Serial.println("done.");   
        gslc_ElemXCheckboxSetState(&m_gui,m_ElemRadio1, true);
        gslc_ElemXCheckboxSetState(&m_gui,m_ElemRadio2, false);
        break;
      case E_ELEM_BTN5:
        play=0;
        DeleteLineFromFile("runstatus.txt", 1);
        runstatus = SD.open("runstatus.txt", FILE_WRITE);
        //Serial.println("Writing to runstatus.txt...");
        runstatus.println(String(play, DEC));
        //close the file:
        runstatus.close();
        //Serial.println("done."); 
        pause_value=stopwatch;
        gslc_ElemXCheckboxSetState(&m_gui,m_ElemRadio1, false);
        gslc_ElemXCheckboxSetState(&m_gui,m_ElemRadio2, true);
        break;
      case E_ELEM_BTN6:
        gslc_SetPageCur(&m_gui, E_PG2);
        break;
      case E_ELEM_BTN7:
        gslc_SetPageCur(&m_gui, E_PG3);
        break;
      case E_ELEM_BTN11:
        gslc_ElemXTextboxReset(&m_gui,m_pElemTextbox1);
        //concat_relay="\0";
        skip_relay = gslc_ElemXSpinnerGetCounter(&m_gui, &m_sXSpinner5);
        int indexofskippedrelay= skip_relay-1;
        //sprintf(skipped_relay[indexofskippedrelay], "%d", skip_relay);
        //skipped_relay[skip_relay-1]=String(skip_relay);
        if (skip_relay>0)
        {
          skipped_relay[indexofskippedrelay]= !skipped_relay[indexofskippedrelay];
          }
          Serial.print("boolean array is ");
        for (int i=0;i<18;i++)
        {
          Serial.print(skipped_relay[i]);
          }
          Serial.println("Done printing boolean");
          int x=0;
          int y=0;
          Serial.print("printing actxt2 ");
          while(1)
        { if (skipped_relay[y]==true)
          {
           acTxt2[x]='1';
          }
          else
          {
            acTxt2[x]='0';
            }
          Serial.print(acTxt2[x]);
          x++;
          y++;
          
          if (y==18)
          {break;}
          acTxt2[x]=',';
          x++;
          
          }
          Serial.println(" Done printing acTxt2");
        
        //snprintf(acTxt,MAX_STR,"%u",skip_relay);
        //Serial.println(skipped_relay);
        
        //snprintf(acTxt2,MAX_STR,"%s",*skipped_relay);
        //Serial.println("value of acTxt2 is "+acTxt2);
        //snprintf(acTxt,MAX_STR,concat_relay);
        gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,acTxt2);
        //gslc_ElemXTextboxAdd(&m_gui,m_pElemTextbox1,*skipped_relay);
        break;
      case E_ELEM_BTN12:
        gslc_SetPageCur(&m_gui, E_PG_MAIN);
        break;
      case E_ELEM_BTN13:
        break;
      case E_ELEM_BTN14:
        break;
      case E_ELEM_BTN15:
        gslc_SetPageCur(&m_gui, E_PG_MAIN);
        break;
//<Button Enums !End!>
default:
        break;
    }
  }

the entire code is also attached.

sequence_controller_2.ino (19.1 KB)

The compiler has problems when you declare a variable with an initialization value inside a case statement. Enclose the entire case in curly brackets to limit the scope of the variable to that particular case.

This is the problem:

        int indexofskippedrelay= skip_relay-1;

If you have your compiler warnings set to "All" (which you SHOULD) you will get a warning message telling you that all of the cases after that line are jumping past a variable initialization. No case beyond that point will be reachable.

Putting brackets around that case will work. Another way, don't initialize the variable in the declaration:

        int indexofskippedrelay;
      indexofskippedrelay = skip_relay-1;

f you have your compiler warnings set to "All" (which you SHOULD) you will get a warning message telling you that all of the cases after that line are jumping past a variable initialization.

Once upon a time in a previous IDE release that would have counted as an error and stopped compilation. The error message was rather cryptic but at least you knew that there was a problem

Then somebody "improved" the compiler or the options used and now it will blithely compile code that will not work as intended.

Is this progress, I wonder ?

that is one weird thing that university didn't teach me.

Curly braces around the entire case solved it.
Thank you everyone for pointing it out.
I was scratching head thinking my mega2560 was getting its RAM filled up because this program eats up 66% of RAM on compilation so I thought may be it was eating up all the ram in runtime and acting weird upon loading page 3.

rvxfahim:
that is one weird thing that university didn't teach me.

Perhaps because incorporating lengthy nested control structures in a 'case', is not really very good design. Calling it as a function from the 'case' would be more favourable, if only from the viewpoint of readability.

Hehehe! I made the same question 1 week ago. !!!

Dont initialize variables inside case:
Cheers