'state' was not declared in this scope

Hi, I'm new to programming. I don't have an Arduino kit, so I made this code at Tinkercad. I downloaded onto the Arduino software. Before I could upload it, however, I decided to verify it. One problem I have is the error message saying 'state' was not declared in this scope. Help is deeply appreciated.
The code is for a Serial Monitor with 4 LED lights:

int blueLED = 8;
int whiteLED = 9;
int greenLED = 10;
int orangeLED = 11;

void setup()
{
  pinMode(blueLED, OUTPUT);
  pinMode(whiteLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(orangeLED, OUTPUT);
  
  Serial.begin(9600);
  while(!Serial);
  Serial.println("Input the corresponding numbers to do actions:");
  Serial.println("  [1] Turn on Blue LED");
  Serial.println("  [2] Turn on White LED");
  Serial.println("  [3] Turn on Green LED");
  Serial.println("  [4] Turn on Orange LED");
  Serial.println("  [5] Turn off LED");
}

void loop()
{
  if(Serial.available()){
    int state = Serial.parseInt();
  } 
  if(state==1){
    digitalWrite(blueLED, HIGH);
    digitalWrite(whiteLED, LOW);
    digitalWrite(greenLED, LOW);
    digitalWrite(orangeLED,LOW);
   }
   if(state==2){
     digitalWrite(whiteLED, HIGH);
     digitalWrite(blueLED, LOW);
     digitalWrite(greenLED, LOW);
     digitalWrite(orangeLED, LOW);
   }
    if(state==3){
      digitalWrite(greenLED, HIGH);
      digitalWrite(blueLED, LOW);
      digitalWrite(whiteLED, LOW);
      digitalWrite(orangeLED, LOW);
   }   
    if(state==4){
      digitalWrite(orangeLED, HIGH);
      digitalWrite(blueLED, LOW);
      digitalWrite(whiteLED, LOW);
      digitalWrite(greenLED, LOW);
   }
    if(state==5){
      digitalWrite(blueLED, LOW);
      digitalWrite(whiteLED, LOW);
      digitalWrite(greenLED, LOW);
      digitalWrite(orangeLED, LOW);
    }   
 }

Because state is declared inside a pair of { and }, its scope is limited to inside them, not loop() as a whole.

{
    int state = Serial.parseInt();
}

You could declare it just above that and keep its scope in loop(), or if you may need it in another function later, put it outside of loop() and make it global.

It is solved. Now another problem came up. The error message says: expected '}' at end of input. How do I solve this?

noobieguy:
How do I solve this?

Post the code and full error message that it produced.

It's obviously not the code you already gave, since that compiled when I moved state's declaration.

Nobody can help with code they can't see....

Ok then

int blueLED = 8;
int whiteLED = 9;
int greenLED = 10;
int orangeLED = 11;

void setup()
{
  pinMode(blueLED, OUTPUT);
  pinMode(whiteLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(orangeLED, OUTPUT);
  
  Serial.begin(9600);
  while(!Serial);
  Serial.println("Input the corresponding numbers to do actions:");
  Serial.println("  [1] Turn on Blue LED");
  Serial.println("  [2] Turn on White LED");
  Serial.println("  [3] Turn on Green LED");
  Serial.println("  [4] Turn on Orange LED");
  Serial.println("  [5] Turn off LED");
}

void loop(){
  if(Serial.available()){
    int state = Serial.parseInt();
   
  if(state==1){
    digitalWrite(blueLED, HIGH);
    digitalWrite(whiteLED, LOW);
    digitalWrite(greenLED, LOW);
    digitalWrite(orangeLED,LOW);
   }
   if(state==2){
     digitalWrite(whiteLED, HIGH);
     digitalWrite(blueLED, LOW);
     digitalWrite(greenLED, LOW);
     digitalWrite(orangeLED, LOW);
   }
    if(state==3){
      digitalWrite(greenLED, HIGH);
      digitalWrite(blueLED, LOW);
      digitalWrite(whiteLED, LOW);
      digitalWrite(orangeLED, LOW);
   }   
    if(state==4){
      digitalWrite(orangeLED, HIGH);
      digitalWrite(blueLED, LOW);
      digitalWrite(whiteLED, LOW);
      digitalWrite(greenLED, LOW);
   }
    if(state==5){
      digitalWrite(blueLED, LOW);
      digitalWrite(whiteLED, LOW);
      digitalWrite(greenLED, LOW);
      digitalWrite(orangeLED, LOW);
    }    
  }

The problem is about the curly bracket at the bottom

The last one you have matches this:

if(Serial.available()){

You don't have one to match this:

void loop()
{

So add another one...

What you did by removing the { that was here:

void loop(){
 if(Serial.available()){
   int state = Serial.parseInt();
  xxxxxx
 if(state==1){

... had the effect of unbalancing them, since it was this ones's buddy:

 if(Serial.available()){

Fixed it. Now another problem came up again. The ‘state’ at ‘(state==5)’ says it’s not declared in this scope.

This whole thing is becoming a whack-a-mole.

If you needed to post the code the previous time, why would you not this time?

 if(state==5){ X
      digitalWrite(blueLED, LOW);
      digitalWrite(whiteLED, LOW);
      digitalWrite(greenLED, LOW);
      digitalWrite(orangeLED, LOW);
    }    
  }

It states at X: 'state' was not declared in this scope.

... the whole sketch

(deleted)

I'm not really good at spotting differences when it comes to coding as I'm new to this.

So post the code that you gave a snippet of some hours ago that gives that last error to do with state 5 or something.

But post the whole sketch, and the actual error.

int blueLED = 8;
int whiteLED = 9;
int greenLED = 10;
int orangeLED = 11;

void setup()
{
  pinMode(blueLED, OUTPUT);
  pinMode(whiteLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(orangeLED, OUTPUT);
  
  Serial.begin(9600);
  while(!Serial);
  Serial.println("Input the corresponding numbers to do actions:");
  Serial.println("  [1] Turn on Blue LED");
  Serial.println("  [2] Turn on White LED");
  Serial.println("  [3] Turn on Green LED");
  Serial.println("  [4] Turn on Orange LED");
  Serial.println("  [5] Turn off LED");
}

void loop(){
  if(Serial.available()){
    int state = Serial.parseInt();
  }
  if(state==1){
    digitalWrite(blueLED, HIGH);
    digitalWrite(whiteLED, LOW);
    digitalWrite(greenLED, LOW);
    digitalWrite(orangeLED,LOW);
   }
   if(state==2){
     digitalWrite(whiteLED, HIGH);
     digitalWrite(blueLED, LOW);
     digitalWrite(greenLED, LOW);
     digitalWrite(orangeLED, LOW);
   }
    if(state==3){
      digitalWrite(greenLED, HIGH);
      digitalWrite(blueLED, LOW);
      digitalWrite(whiteLED, LOW);
      digitalWrite(orangeLED, LOW);
   }   
    if(state==4){
      digitalWrite(orangeLED, HIGH);
      digitalWrite(blueLED, LOW);
      digitalWrite(whiteLED, LOW);
      digitalWrite(greenLED, LOW);
   }
    if(state==5){ <-- this line here
      digitalWrite(blueLED, LOW);
      digitalWrite(whiteLED, LOW);
      digitalWrite(greenLED, LOW);
      digitalWrite(orangeLED, LOW);
    }    
  }

At ‘if(state==5){’, it said 'state was not declared in this scope.

See reply #1, and then reply #11

(deleted)