int within if not working

Hello, I have been making a ridiculously overcomplicated program for having a failsafe, as my motors failed way too many times during nothing but net. I have some integers being changed withing if loops. I should note that the ints that arent working have gray x’s in front. Here is my code:


task usercontrol()
{
  // User control code here, inside the loop
//int x declaration, this works
int x = 0;
//extremely overcomplicated code for a failsafe
  while(x == 0)
  {
  	motor(motorFR) = vexRT[Ch2];
    motor(motorBR) = vexRT[Ch2];
    motor(motorFL) = vexRT[Ch3];
    motor(motorBL) = vexRT[Ch3];
    if(vexRT[Btn8U]){
    	//gray x, assuming its due to it being in the if statement
    	int x = 1;
    }
    else if(vexRT[Btn8D]){
    	//gray x, assuming its due to it being in the if statement
    	int x = 2;
    }
    else {

  }

  }
  while(x == 1){
  	motor(motorFR) = vexRT[Ch2];
  	motor(motorFL) = vexRT[Ch3];
  	if(vexRT[Btn8L]){
    	//gray x, assuming its due to it being in the if statement
    	int x = 0;
    }
    else if(vexRT[Btn8D]){
    	//gray x, assuming its due to it being in the if statement
    	int x = 2;
    }
    else {

  }
	}
	while(x == 2) {
		motor(motorBR) = vexRT[Ch2];
		motor(motorBL) = vexRT[Ch3];
		if(vexRT[Btn8U]){
    	//gray x, assuming its due to it being in the if statement
    	int x = 1;
    }
    else if(vexRT[Btn8L]){
    	//gray x, assuming its due to it being in the if statement
    	int x = 0;
    }
    else {

  }
	}
}

A grey X is an information message, lets see what it says.


**Info***:'x' is written but has no read references

The problem is you are defining x within the while loop as “int x”, this is known as block scope and allows variable name reuse. The compiler is telling you that you have defined a variable called x but never use it within that block. This is what I think you really wanted.

task usercontrol()
{
  // User control code here, inside the loop
  //int x declaration, this works
  int x = 0;
  
  //extremely overcomplicated code for a failsafe
  while(x == 0)
  {
    motor(motorFR) = vexRT[Ch2];
    motor(motorBR) = vexRT[Ch2];
    motor(motorFL) = vexRT[Ch3];
    motor(motorBL) = vexRT[Ch3];
    if(vexRT[Btn8U]){
      x = 1;
    }
    else if(vexRT[Btn8D]){
      x = 2;
    }
    else {
    // ??
    }

  }
  while(x == 1){
    motor(motorFR) = vexRT[Ch2];
    motor(motorFL) = vexRT[Ch3];
    if(vexRT[Btn8L]){
      x = 0;
    }
    else if(vexRT[Btn8D]){
      x = 2;
    }
    else {
    // ??
    }
  }
  
  while(x == 2) {
    motor(motorBR) = vexRT[Ch2];
    motor(motorBL) = vexRT[Ch3];
    if(vexRT[Btn8U]){
      x = 1;
    }
    else if(vexRT[Btn8L]){
      x = 0;
    }
    else {
    // ??
    }
  }
}