'else' without a previous 'if' Help

I'am pretty new to Arduino and using c++ but can some help me fix this error?
char command;
char data = 0;
int motorPlus1 = 5;
int motorMinus1 = 6;
int motorPlus2 = 3;
int motorMinus2 = 2;
int motorPlus3 = 8;
int motorMinus3 = 9;
int motorPlus4 = 10;
int motorMinus4= 11;

void setup() {
Serial.begin(9600);
pinMode(motorPlus1,OUTPUT);
pinMode(motorMinus1,OUTPUT);
pinMode(motorPlus2,OUTPUT);
pinMode(motorMinus2,OUTPUT);
pinMode(motorPlus3,OUTPUT);
pinMode(motorMinus3,OUTPUT);
pinMode(motorPlus4,OUTPUT);
pinMode(motorMinus4,OUTPUT);
}

void loop() {
if(Serial.available() > 0)
data = Serial.read();
Serial.print(data);
Serial.print("\n");

if(data == '1') //foward

digitalWrite(motorPlus1,HIGH);
digitalWrite(motorMinus1,LOW);
digitalWrite(motorPlus2,HIGH);
digitalWrite(motorMinus2,LOW);
digitalWrite(motorPlus3,HIGH);
digitalWrite(motorMinus3,LOW);
digitalWrite(motorPlus4,HIGH);
digitalWrite(motorMinus4,LOW);

else if (data == '0') //off

digitalWrite(motorPlus1,LOW);
digitalWrite(motorMinus1,LOW);
digitalWrite(motorPlus2,LOW);
digitalWrite(motorMinus2,LOW);
digitalWrite(motorPlus3,LOW);
digitalWrite(motorMinus3,LOW);
digitalWrite(motorPlus4,LOW);
digitalWrite(motorMinus4,LOW);

}

Spaces don't delimit code blocks. You need to use {} with your if and else if.

You need to put braces like { and } around the parts you want to be going with the if statements. Without them only the next line belongs to the if and the rest are all run no matter the condition.

Go through the first few lessons of any online C++ tutorial and you should be able to pick up the basic stuff like this pretty quick. Some of C++ can get pretty hard, but this kind of stuff is just stuff you need to see one time so you know how it is supposed to look.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

This link may help.

Tom... :slight_smile: