Go Down

Topic: expected initializer before 'int'. (Read 2512 times) previous topic - next topic

yazidabdulhamid

void setup()
{
  Serial.begin(9600); //Set serial baud rate to 9600 bps
  pinMode (10,OUTPUT);
}
void loop ()
  int (valGas , valLDR) ;
    valGas = analogRead(0);
    valLDR = analogRead(1);
 
  Serial.println(valGas,DEC);
  Serial.print (",");
  Serial.println in(valLDR,DEC);
 
if(valGas<600) {
    digitalWrite (10,High);
  } else }
     digitalWrite (10,Low);
   }
if (valLDR<400) {
   digitalWrite(11,High);
} else }
   digitalWrite(11,Low);
   }
  delay (100) ;
  }

i get error when i want to compile this code..can anybody help me to see what is wrong with my code?

mmcp42

Code: [Select]
void setup()
{
  Serial.begin(9600); //Set serial baud rate to 9600 bps
  pinMode (10,OUTPUT);
}
void loop ()
  int (valGas , valLDR) ;
    valGas = analogRead(0);
    valLDR = analogRead(1);

  Serial.println(valGas,DEC);
  Serial.print (",");
  Serial.println in(valLDR,DEC);

if(valGas<600) {
    digitalWrite (10,High);
  } else }
     digitalWrite (10,Low);
   }
if (valLDR<400) {
   digitalWrite(11,High);
} else }
   digitalWrite(11,Low);
   }
  delay (100) ;
  }


makes it easier to read

try

Code: [Select]
int valGas;
int valLDR;


or
Code: [Select]
int valGas , valLDR ;

no brackets!
there are only 10 types of people
them that understands binary
and them that doesn't

yazidabdulhamid

sorry, my bad.

i try both ways..but still error.

mmcp42

you also need
Code: [Select]

void loop
{
... your code
}


you left out the "{" and "}"
there are only 10 types of people
them that understands binary
and them that doesn't

yazidabdulhamid

#4
Sep 07, 2011, 01:17 pm Last Edit: Sep 07, 2011, 01:23 pm by yazidabdulhamid Reason: 1
Code: [Select]
  Serial.println(valgas,DEC);
  Serial.print (",");
  Serial.println(valLDR,DEC);


is this part right?

yazidabdulhamid

Code: [Select]
if(valGas<600) {
    digitalWrite (10,High);
  } else }
     digitalWrite (10,Low);
   }
if (valLDR<400) {
   digitalWrite(11,High);
} else }
   digitalWrite(11,Low);
   }
  delay (100) ;
  }


is this the right way to write if-else statement?

PaulS

Quote
is this part right?

If you want that extra carriage return/line feed in the middle, yes. Seems strange to me, though.

Quote
is this the right way to write if-else statement?

Sort of. The braces are not all facing the right way, and they are not all on separate lines where they belong (in my opinion).
Code: [Select]
if(valGas<600)
{
    digitalWrite (10,High);
}
else
{
   digitalWrite (10,Low);
}

yazidabdulhamid

Code: [Select]
void setup()
{
  Serial.begin(9600); //Set serial baud rate to 9600 bps
  pinMode (10,OUTPUT);
}
void loop ()
{
  int valgas , valLDR;
    valgas = analogRead(0);
    valLDR = analogRead(1);

  Serial.println(valgas,DEC);
  Serial.print (",");
  Serial.println (valLDR,DEC);
}
if(valGas<600)
{
    digitalWrite (10,High);
}
else
{
   digitalWrite (10,Low);
}
if (valLDR<400)
{
   digitalWrite(11,High);
}
else
{
   digitalWrite(11,Low);
   }
  delay (100) ;
  }


this is my current coding,
however, i still got error at this part

Code: [Select]
if(valGas<600)
{
    digitalWrite (10,High);
}
else
{
   digitalWrite (10,Low);
}
if (valLDR<400)
{
   digitalWrite(11,High);
}
else
{
   digitalWrite(11,Low);
   }
  delay (100) ;
  }

mmcp42

look at you code
you have { and } all over the place

you should have
Code: [Select]

void loop()
{
  .. all of your code
  if (condition)
  {
    .. true code
  }
  else
  {
    ... false code
  }
}


use indents to help you keep track
and check matching { and }
there are only 10 types of people
them that understands binary
and them that doesn't

PaulS

The IDE has a menu, Tools. On that menu, there is an item, Auto Format. Do your self a favor. Use that tool. Often. It will fix all your random indents. It will also line up all your curly braces, indenting in and out as required. When it gets to the end of a function, and there are still closing } left over, they will bot be indented, leaving you a big clue that the number of open and close braces don't match.

ALL code needs to be inside a function. Not all of yours is. Why not?

Go Up