Unmatched left brace and unexpected right brace during parsing, but they're actually present?

I’m trying to make a robot with RobotC and it’s saying there’s an unmatched left brace and an unexpected right brace, but those are the only two braces I have.

#pragma config(Sensor, in1,    soweli,         sensorPotentiometer)
#pragma config(Sensor, dgtl1,  waso,           sensorTouch)
#pragma config(Motor,  port2,           iloSike,       tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/*===============================FUNCTIONS==================================*/

/*
void goingUp(int level)
{
startMotor(iloSike, 30);
wait(level * 3.75 + 0.5);
}

//Not being used but is being kept around in case i need it

void goingDown(int level)
{
startMotor(iloSike, -30);
wait(level * 3.75 + 0.25);
}
*/

/*===============================VARIABLES==================================*/
task main() 
{								//The beginning.
	int level;

	level = 1;

	/*=================================CODE=====================================*/

	if (SensorValue(soweli)>= 0 && SensorValue(soweli)<= 1365 && SensorValue(waso)== 1 && level != 1)

	if (SensorValue(soweli)>= 1366 && SensorValue(soweli)<= 2730 && SensorValue(waso)== 1 && level != 2)

	if (SensorValue(soweli)>= 0 && SensorValue(soweli)<= 1365 && SensorValue(waso)== 1 && level != 3) 
}

Any help would be greatly appreciated, as both me and my teacher don’t understand the issue here.

Robot C expects you to have something after your if statement to run if the condition is true, but there isn’t anything there. You need to add curly brackets after the if like this:

If (condition) {
    // Code to run if condition is true…
}
4 Likes

In C/C++, control flow statements such as if and while will execute the next “statement” where a “statement” can either be a single line or a set of statements grouped in the braces.

So, your code is actually trying to do:

task main() 
{								//The beginning.
	int level;

	level = 1;

	/*=================================CODE=====================================*/

	if (SensorValue(soweli)>= 0 && SensorValue(soweli)<= 1365 && SensorValue(waso)== 1 && level != 1)
   	  if (SensorValue(soweli)>= 1366 && SensorValue(soweli)<= 2730 && SensorValue(waso)== 1 && level != 2)
   	    if (SensorValue(soweli)>= 0 && SensorValue(soweli)<= 1365 && SensorValue(waso)== 1 && level != 3) 
}

So the fact that the character immediately after the 3rd if statement is a closing bracket is why the compiler is telling you that you have “unmatched” brackets.

what you likely want is something more like:

task main() 
{								//The beginning.
	int level;

	level = 1;

	/*=================================CODE=====================================*/

	if (SensorValue(soweli)>= 0 && SensorValue(soweli)<= 1365 && SensorValue(waso)== 1 && level != 1) {
      // Do something
    } else if (SensorValue(soweli)>= 1366 && SensorValue(soweli)<= 2730 && SensorValue(waso)== 1  {&& level != 2) {
      // Do something different
    } else if (SensorValue(soweli)>= 0 && SensorValue(soweli)<= 1365 && SensorValue(waso)== 1 &&  level != 3)  {
      // Do something completely different
    }
}

Although I’m not completely sure whether separating out the 2nd and 3rd if statements to be elses of the first is your intention or not.

2 Likes