My 'else' statement is not working. Anyone know why?

#pragma config(Motor,  port2,           motorRIGHT,    tmotorVex393_MC29, openLoop)
#pragma config(Motor,  port3,           motorLEFT,     tmotorVex393_MC29, openLoop, reversed)
#pragma config(Motor,  port4,           motorARM,      tmotorVex269_MC29, openLoop)
#pragma config(Motor,  port5,           motorWHEEL,    tmotorVex269_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{	

	while(1==1)
	{	motor[motorLEFT] = vexRT[Ch3];
		motor[motorRIGHT] = vexRT[Ch2];


		if(vexRT[Btn6U] == 1)
		{
			motor[motorARM] = 127;
		}

		else if (vexRT[Btn6D] == 1)
		{
			motor[motorARM] = -127;

			else
			{
				motor[motorARM] = 0;
			}



			if(vexRT[Btn5U] == 1)
			{
				motor[motorWHEEL] = 127;
			}

			else if (vexRT[Btn5D] == 1)
			{
				motor[motorWHEEL] = -127;

				else
				{
					motor[motorWHEEL] = 0;
				}
			}


		}

It appears to me like a couple of your if statements do not have closing brackets. This could be the problem.

3 Likes

Your else if statements have no closing curly brackets

1 Like

You’re actually missing 2 closing curly brackets at the end… ones for your while loop and task main.

Where should i add the brackets?

Put the 2 closing curly brackets at the very bottom of the code. They correspond to the task and the while loop. All of your if and if/else have corresponding open and close brackets so they’re good.

Others have done an excellent job of helping out. For anyone else having a related problem, make sure that all of your starting curly bracket and parantheses have a corresponding ending curly bracket or paranthesis. A fast way to check is by putting your cursor next to the curly bracket - your IDE should automatically highlight the corresponding one in your code.

Also, a note about debugging. If you run your code and see an error, oftentimes the error explains what is happening. When I ran your code, for example, I see the following message:

src/main.cpp:33:2: note: to match this '{'
        {       motor[motorLEFT] = vexRT[Ch3];
        ^
src/main.cpp:69:4: error: expected '}'
                }
                 ^

Even though the exact wording may be a bit confusing at times, it’s usually possible to get a good idea of what’s causing the problem. Here, it seems like my opening curly bracket on line 33 expects a closing curly bracket, which is exactly what others identified.

2 Likes