Announcement

Collapse
No announcement yet.

Nested IF statement fail!

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Nested IF statement fail!

    Hi all. I thought I understood how nested IF and ELSE IF statements work, but I guess I don't!

    Could someone please tell me why my code compiles with the lines shown in red commented out, but with lines re-instated, compiling fails with the message: expected '}' before 'else' ?

    My program works fine with the two lines commented out, so I've just posted up the snippet that doesn't work:

    Code:
    {
      int rockersw_val;          //holds the status of Rockerswitch0
      genieFrame Event;    
      genie.DequeueEvent(&Event); // Remove the next queued event from the buffer, and process it below
      
      //Evaluate the event
      if (genie.EventIs(&Event, GENIE_REPORT_OBJ, GENIE_OBJ_ROCKERSW, 0x00)){ //if the event is a REPORT_OBJ from Rockerswitch0
        rockersw_val = genie.GetEventData(&Event);  //extract the MSB and LSB values and pass them to rockersw_val
        if (rockersw_val == 0)                     //if Rockerswitch0 is off
          flag = 0;                                //"turn off" the voltmeter
          //VMeterColour = 0x00;                     //Use switch to also change gauge colour to black
       else if (rockersw_val == 1)                //if Rockerswitch0 is on
          flag = 1;                                //"turn on" the voltmeter
          //VMeterColour = 0x01;                        //Use switch to also change gauge colour to white
        } 
           
      }
    Thanks! :D

  • #2
    Hello,

    I believe it is because there is more than one command after the condition and because of that it should be inside a { .... }

    This will work.
    Code:
     {  
    int rockersw_val;          //holds the status of Rockerswitch0
      genieFrame Event;      
      genie.DequeueEvent(&Event); // Remove the next queued event from the buffer, and process it below
         //Evaluate the event
      if (genie.EventIs(&Event, GENIE_REPORT_OBJ, GENIE_OBJ_ROCKERSW, 0x00)){ //if the event is a REPORT_OBJ from Rockerswitch0
        rockersw_val = genie.GetEventData(&Event);  //extract the MSB and LSB values and pass them to rockersw_val
        if (rockersw_val == 0){                      //if Rockerswitch0 is off      
          flag = 0;                                          //"turn off" the voltmeter      
         VMeterColour = 0x00;                     //Use switch to also change gauge colour to black
        } else if (rockersw_val == 1){             //if Rockerswitch0 is on
          flag = 1;                                          //"turn on" the voltmeter      
        VMeterColour = 0x01;                      //Use switch to also change gauge colour to white
        }
       }          
     }
    Paul

    Comment


    • #3
      Ah I see! Great now it works perfectly!

      Thanks

      Comment

      Working...
      X