ROBOTC.net forums
http://robotc.net/forums/

Unexpected else
http://robotc.net/forums/viewtopic.php?f=11&t=11024
Page 1 of 1

Author:  robot5 [ Wed Apr 29, 2015 4:07 pm ]
Post subject:  Unexpected else

Hi!
I'm brand new to programming. I am having problems with else in while loops. I can't figure it out. Everything works except for when I try to compile it to my robot. Then it says that the else in red print is unexpected. If you know anything that could help us that would be great! Thanks!
Code:
 
#pragma config(Sensor, dgtl1,  rightEncoder,   sensorQuadEncoder)
#pragma config(Sensor, dgtl3,  leftEncoder,    sensorQuadEncoder)
#pragma config(Sensor, dgtl8,  sonarSensor,    sensorSONAR_cm)
#pragma config(Motor,  port2,           rightMotor,    tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
   wait1Msec(2000);


      
while(1 == 1)      // Creates an infinite loop, since "true" always evaluates to true
   {
      if(SensorValue[sonarSensor] > 50)
      {
         {
   wait1Msec(2000);                     // Robot waits for 2000 milliseconds before executing program

   SensorValue[rightEncoder] = 0;     // Set the encoder so that it starts counting at 0
   SensorValue[leftEncoder]  = 0;     // Set the encoder so that it starts counting at 0

         while(1 == 1)      // Creates an infinite loop, since "true" always evaluates to true
            {
            if(SensorValue[rightEncoder] == SensorValue[leftEncoder]) // If rightEncoder has counted the same amount as leftEncoder:
               {
               // Move Forward
               motor[rightMotor] = 80;          // Right Motor is run at power level 80
               motor[leftMotor]  = 80;          // Left Motor is run at power level 80
               }
               else if(SensorValue[rightEncoder] > SensorValue[leftEncoder])   // If rightEncoder has counted more encoder counts
               {
                  // Turn slightly right
                  motor[rightMotor] = 60;          // Right Motor is run at power level 60
                  motor[leftMotor]  = 80;          // Left Motor is run at power level 80
               }
               else   // Only runs if leftEncoder has counted more encoder counts
               {
                  // Turn slightly left
                  motor[rightMotor] = 80;          // Right Motor is run at power level 80
                  motor[leftMotor]  = 60;          // Left Motor is run at power level 60
               }
            }
         }
      [color=#FF0000]else if(SensorValue[sonarSensor] < 50)   // If it sees a wall[/color]
      {
            // Turn right
         motor[rightMotor] = -80;          // Right Motor is run at power level 80 backwards
         motor[leftMotor]  = 80;          // Left Motor is run at power level 80
      }
      else
      {
         // Turn  left
         motor[rightMotor] = 80;          // Right Motor is run at power level 80
         motor[leftMotor]  = -80;          // Left Motor is run at power level 80 backwards
      }
   }
}
}         

Author:  Tabor473 [ Tue May 19, 2015 3:48 pm ]
Post subject:  Re: Unexpected else

So to help with debugging I am going to move all the encoder stuff into its own function


Code:
#pragma config(Sensor, dgtl1,  rightEncoder,   sensorQuadEncoder)
#pragma config(Sensor, dgtl3,  leftEncoder,    sensorQuadEncoder)
#pragma config(Sensor, dgtl8,  sonarSensor,    sensorSONAR_cm)
#pragma config(Motor,  port2,           rightMotor,    tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
void EncoderStuff(){
    wait1Msec(2000);                     // Robot waits for 2000 milliseconds before executing program

   SensorValue[rightEncoder] = 0;     // Set the encoder so that it starts counting at 0
   SensorValue[leftEncoder]  = 0;     // Set the encoder so that it starts counting at 0

         while(1 == 1)      // Creates an infinite loop, since "true" always evaluates to true
            {
            if(SensorValue[rightEncoder] == SensorValue[leftEncoder]) // If rightEncoder has counted the same amount as leftEncoder:
               {
               // Move Forward
               motor[rightMotor] = 80;          // Right Motor is run at power level 80
               motor[leftMotor]  = 80;          // Left Motor is run at power level 80
               }
               else if(SensorValue[rightEncoder] > SensorValue[leftEncoder])   // If rightEncoder has counted more encoder counts
               {
                  // Turn slightly right
                  motor[rightMotor] = 60;          // Right Motor is run at power level 60
                  motor[leftMotor]  = 80;          // Left Motor is run at power level 80
               }
               else   // Only runs if leftEncoder has counted more encoder counts
               {
                  // Turn slightly left
                  motor[rightMotor] = 80;          // Right Motor is run at power level 80
                  motor[leftMotor]  = 60;          // Left Motor is run at power level 60
               }
            }
}





//new main task
task main()
{
   wait1Msec(2000);


     
while(1 == 1)      // Creates an infinite loop, since "true" always evaluates to true
   {
      if(SensorValue[sonarSensor] > 50)
      {
         {
              EncoderStuff();
         }
      else if(SensorValue[sonarSensor] < 50)   // If it sees a wall[/color]
      {
            // Turn right
         motor[rightMotor] = -80;          // Right Motor is run at power level 80 backwards
         motor[leftMotor]  = 80;          // Left Motor is run at power level 80
      }
      else
      {
         // Turn  left[/color]
         motor[rightMotor] = 80;          // Right Motor is run at power level 80
         motor[leftMotor]  = -80;          // Left Motor is run at power level 80 backwards
      }
   }
}
}     


Now it is very easy to see that

Code:
 if(SensorValue[sonarSensor] > 50)
      {
         {

Has 2 open brackets. Else statements require an if to have directly preceded the else statement. Removing one of the opening brackets as well as one of the closing brackets at the end of the program should give you the desired outcome.

Author:  robot5 [ Tue May 26, 2015 7:49 pm ]
Post subject:  Re: Unexpected else

Thank you so much! I get it now.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/