error code expected initializer

  if (digitalRead (36) == LOW)
  
{ Serial.println ("Switch closed.");
  delay (1000);


  analogWrite(RPWM_Output, 100);
}

void loop()
{

I'm sorry, I refuse to believe this code has been auto-formatted.

OP's code, properly formatted,

#define ENCODER0PINA         20      // this pin needs to support interrupts
#define ENCODER0PINB         18      // interuppt
#define CPR                  400     // encoder cycles per revolution
#define CLOCKWISE            1       // direction constant
#define COUNTER_CLOCKWISE    2       // direction constant

// variables modified by interrupt handler must be declared as volatile
volatile long encoder0Position = 0;
volatile long interruptsReceived = 0;

// track direction: 0 = counter-clockwise; 1 = clockwise
short currentDirection = CLOCKWISE;

// track last position so we know whether it's worth printing new output
long previousPosition = 0;

int SENSOR_PIN = 0; // center pin of the potentiometer

int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)

void setup()
{ 
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
    pinMode(RPWM_Output, OUTPUT);
    pinMode(LPWM_Output, OUTPUT);
    pinMode(36, INPUT_PULLUP); //home switch
    const byte interruptPin = 20;

    // inputs
    pinMode(ENCODER0PINA, INPUT_PULLUP);
    pinMode(ENCODER0PINB, INPUT_PULLUP);

    // interrupts
    attachInterrupt(3, onInterrupt, RISING);
    // enable diagnostic output
    Serial.begin (9600);
    Serial.println("\n\n\n");
    Serial.println("Ready.");

    //drive to home switch
    analogWrite(LPWM_Output, 100);

    if (digitalRead (36) == LOW)
    { 
        Serial.println ("Switch closed.");
        delay (1000);
        analogWrite(RPWM_Output, 100);
    }

    void loop()
    {
        if (encoder0Position != previousPosition)
        Serial.print(encoder0Position, DEC);
        Serial.print("\t");
        Serial.print(currentDirection == CLOCKWISE ? "clockwise" : "counter-clockwise");
        Serial.print("\t");
        Serial.println(interruptsReceived, DEC);
        previousPosition = encoder0Position;
    }

    // interrupt function needs to do as little as possible
    void onInterrupt()

    // read both inputs
    int a = digitalRead(ENCODER0PINA);
    int b = digitalRead(ENCODER0PINB);

    if (a = b )
    {
        // b is leading a (counter-clockwise)
        encoder0Position--;
        currentDirection = COUNTER_CLOCKWISE;
    }
    else
    {
        // a is leading b (clockwise)
        encoder0Position++;
        currentDirection = CLOCKWISE;
    }

    // track 0 to 20000
    encoder0Position = encoder0Position % CPR;

    // track the number of interrupts
    interruptsReceived++;
    //read the pushbutton value into a variable
    { int sensorVal = digitalRead(36);
        //print out the value of the pushbutton
        Serial.println(sensorVal);
    }
    int sensorValue = analogRead(SENSOR_PIN);

    // sensor value is in the range 0 to 1023
    // the lower half of it we use for reverse rotation; the upper half for forward rotation
    if (sensorValue < 512)
    {
        // reverse rotation
        int reversePWM = -(sensorValue - 511) / 2;
        analogWrite(LPWM_Output, 0);
        analogWrite(RPWM_Output, reversePWM);
    }
    else
    {
        // forward rotation
        int forwardPWM = (sensorValue - 512) / 2;
        analogWrite(LPWM_Output, forwardPWM);
        analogWrite(RPWM_Output, 0);
    }

Error messages,

Arduino: 1.8.6 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

WARNING: Category '' in library ArduinoUnit is not valid. Setting to 'Uncategorized'
Build options changed, rebuilding all
C:\Users\Watson\Documents\Arduino\forum\forum.ino: In function 'void setup()':

forum:36:24: error: 'onInterrupt' was not declared in this scope

     attachInterrupt(3, onInterrupt, RISING);

                        ^

forum:53:5: error: a function-definition is not allowed here before '{' token

     {

     ^

forum:67:5: error: expected initializer before 'int'

     int a = digitalRead(ENCODER0PINA);

     ^

forum:70:9: error: 'a' was not declared in this scope

     if (a = b )

         ^

forum:110:5: error: expected '}' at end of input

     }

     ^

exit status 1
'onInterrupt' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Notice how the error messages are the same, even with properly formatted code. However, if you look at properly formatted code, the mis-matched braces are still clearly evident.

Just for interest, clean up the braces, as it has been recommended from the beginning, and this is the compiler response,

Sketch uses 4850 bytes (1%) of program storage space. Maximum is 253952 bytes.
Global variables use 282 bytes (3%) of dynamic memory, leaving 7910 bytes for local variables. Maximum is 8192 bytes.

bmachining:
I have used auto format.

Auto Format applies a consistent format to the code you have written. It does not fix problems, but can help show what might be causing them.

As an example here is a portion of your code Auto Formatted

void setup()
{
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  pinMode(RPWM_Output, OUTPUT);
  pinMode(LPWM_Output, OUTPUT);
  pinMode(36, INPUT_PULLUP); //home switch
  const byte interruptPin = 20;
  // inputs
  pinMode(ENCODER0PINA, INPUT_PULLUP);
  pinMode(ENCODER0PINB, INPUT_PULLUP);
  // interrupts
  attachInterrupt(3, onInterrupt, RISING);
  // enable diagnostic output
  Serial.begin (9600);
  Serial.println("\n\n\n");
  Serial.println("Ready.");
  //drive to home switch
  analogWrite(LPWM_Output, 100);
  if (digitalRead (36) == LOW)
  {
    Serial.println ("Switch closed.");
    delay (1000);
    analogWrite(RPWM_Output, 100);
  }
  
  void loop()
  {
etc, etc

Functions should end with a closing brace on the left margin. Does your code do that when Auto Formatted ? Where does the setup() function end ?

This is auto formatted, and clicked on copy to forum.

#define ENCODER0PINA         20      // this pin needs to support interrupts
#define ENCODER0PINB         18      // interuppt
#define CPR                  400     // encoder cycles per revolution
#define CLOCKWISE            1       // direction constant
#define COUNTER_CLOCKWISE    2       // direction constant

// variables modified by interrupt handler must be declared as volatile
volatile long encoder0Position = 0;
volatile long interruptsReceived = 0;

// track direction: 0 = counter-clockwise; 1 = clockwise
short currentDirection = CLOCKWISE;

// track last position so we know whether it's worth printing new output
long previousPosition = 0;

int SENSOR_PIN = 0; // center pin of the potentiometer

int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)




void setup()

{ Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  pinMode(RPWM_Output, OUTPUT);
  pinMode(LPWM_Output, OUTPUT);
  pinMode(36, INPUT_PULLUP); //home switch
  const byte interruptPin = 20;

  // inputs
  pinMode(ENCODER0PINA, INPUT_PULLUP);
  pinMode(ENCODER0PINB, INPUT_PULLUP);

  // interrupts
  attachInterrupt(3, onInterrupt, RISING);
  // enable diagnostic output
  Serial.begin (9600);
  Serial.println("\n\n\n");
  Serial.println("Ready.");

  //drive to home switch
  analogWrite(LPWM_Output, 100);

  if (digitalRead (36) == LOW)

    Serial.println ("Switch closed.");
  delay (1000);


  analogWrite(RPWM_Output, 100);
}

void loop()
{

  if (encoder0Position != previousPosition)


    Serial.print(encoder0Position, DEC);
  Serial.print("\t");
  Serial.print(currentDirection == CLOCKWISE ? "clockwise" : "counter-clockwise");
  Serial.print("\t");
  Serial.println(interruptsReceived, DEC);
  previousPosition = encoder0Position;

}

// interrupt function needs to do as little as possible
void onInterrupt()

// read both inputs
int a = digitalRead(ENCODER0PINA);
int b = digitalRead(ENCODER0PINB);

if (a == b )
{
  // b is leading a (counter-clockwise)
  encoder0Position--;
  currentDirection = COUNTER_CLOCKWISE;
}
else
{
  // a is leading b (clockwise)
  encoder0Position++;
  currentDirection = CLOCKWISE;
}

// track 0 to 20000
encoder0Position = encoder0Position % CPR;

// track the number of interrupts
interruptsReceived++;
//read the pushbutton value into a variable
{ int sensorVal = digitalRead(36);
  //print out the value of the pushbutton
  Serial.println(sensorVal);
}
int sensorValue = analogRead(SENSOR_PIN);

// sensor value is in the range 0 to 1023
// the lower half of it we use for reverse rotation; the upper half for forward rotation
if (sensorValue < 512)



{
  // reverse rotation
  int reversePWM = -(sensorValue - 511) / 2;
  analogWrite(LPWM_Output, 0);
  analogWrite(RPWM_Output, reversePWM);
}
else
{
  // forward rotation
  int forwardPWM = (sensorValue - 512) / 2;
  analogWrite(LPWM_Output, forwardPWM);
  analogWrite(RPWM_Output, 0);
}

I guess you didn't read the last time, so I'll repeat myself:

After using auto format, did you look at the indentation and compare it to your expected program structure? Did you notice the obvious problems that indentation shows?

If not, then you might read again what slipstick told you:

Use Ctrl-T or Tools/AutFormat to format the code first. Then there are a few basics you can look for on your own. Braces and all other brackets must always be balanced...you need a start { and a finish } or it won't compile. Function definitions like void loop() must always start at the beginning of the line, no spaces. If they don't then your brackets are still in a mess.

Even after the auto format, the formatting of your code is very messy. It's not just about aesthetics. Messy code is difficult to understand and clearly you're having a tremendous difficulty with understanding anything. It's been a couple of days now. Don't fool yourself into thinking you're saving time by writing code that looks like a pile of garbage. This sloppy approach to writing code is only going to cause you a lot of frustration and wasted time.

bmachining:

{ Serial.begin(9600);

Don't ever put code on the same line following a brace. This should look like:

{
  Serial.begin(9600);

bmachining:

  if (digitalRead (36) == LOW)

Serial.println ("Switch closed.");

Always use braces, even if they only contain one statement. It's worth that tiny bit of extra effort to make your code more self documenting and resistant to bugs.

bmachining:

if (sensorValue < 512)

{

Don't leave random blank lines in your code. It's fine to use them intentionally to break your code into logical sections but there is absolutely no valid reason for the three blank lines there.

I think it's evident that OP is not interested in following advice, rather argue in favour of not. I've wasted enough of my time on this one. Even as I got his code working, he'll only see that now if he does it himself.

Im following the advice DK. I have changed brackets to be balanced etc but there must be some unwritten rules with them.

bmachining:
I have changed brackets to be balanced etc

Post the updated code.

bmachining:
I have changed brackets to be balanced etc but there must be some unwritten rules with them.

Certainly not. C++ is extremely well documented and this is one of the absolute basic fundamentals. It's quite simple: Any time you have a { you must also have a }. Every function starts with a { and ends with a }. Don't try to define a function inside another function.

pert, may be simple for those with a bit of experience.

Knowing what does need the { } and what doesnt could be part of it.

#define ENCODER0PINA         20      // this pin needs to support interrupts
#define ENCODER0PINB         18      // interuppt
#define CPR                  400     // encoder cycles per revolution
#define CLOCKWISE            1       // direction constant
#define COUNTER_CLOCKWISE    2       // direction constant

// variables modified by interrupt handler must be declared as volatile
volatile long encoder0Position = 0;
volatile long interruptsReceived = 0;

// track direction: 0 = counter-clockwise; 1 = clockwise
short currentDirection = CLOCKWISE;

// track last position so we know whether it's worth printing new output
long previousPosition = 0;

int SENSOR_PIN = 0; // center pin of the potentiometer

int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)

void setup()
{
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  pinMode(RPWM_Output, OUTPUT);
  pinMode(LPWM_Output, OUTPUT);
  pinMode(36, INPUT_PULLUP); //home switch

  const byte interruptPin = 20;

  // inputs
  pinMode(ENCODER0PINA, INPUT_PULLUP);
  pinMode(ENCODER0PINB, INPUT_PULLUP);

  // interrupts
  attachInterrupt(3, onInterrupt, RISING);
  // enable diagnostic output
  Serial.begin (9600);
  Serial.println("\n\n\n");
  Serial.println("Ready.");

  //drive to home switch
  analogWrite(LPWM_Output, 100);


  if (digitalRead (36) == LOW);

  Serial.println ("Switch closed.");
  delay (1000);
  analogWrite(RPWM_Output, 100);
}

void loop()
{
  if (encoder0Position != previousPosition)

    Serial.print(encoder0Position, DEC);
  Serial.print("\t");
  Serial.print(currentDirection == CLOCKWISE ? "clockwise" : "counter-clockwise");
  Serial.print("\t");
  Serial.println(interruptsReceived, DEC);
  previousPosition = encoder0Position;
}


// interrupt function needs to do as little as possible
void onInterrupt()
{
  // read both inputs
  int a = digitalRead(ENCODER0PINA);
  int b = digitalRead(ENCODER0PINB);

  if (a == b )

    // b is leading a (counter-clockwise)
    encoder0Position--;
  currentDirection = COUNTER_CLOCKWISE;
}
else
{
  // a is leading b (clockwise)
  encoder0Position++;
  currentDirection = CLOCKWISE;
}

// track 0 to 20000
{
  encoder0Position = encoder0Position % CPR;
}

// track the number of interrupts
interruptsReceived++;
//read the pushbutton value into a variable

{
  int sensorVal = digitalRead(36);
  //print out the value of the pushbutton
  Serial.println(sensorVal);


  int sensorValue = analogRead(SENSOR_PIN);

  // sensor value is in the range 0 to 1023
  // the lower half of it we use for reverse rotation; the upper half for forward rotation
  if (sensorValue < 512)

    // reverse rotation
    int reversePWM = -(sensorValue - 511) / 2;
  analogWrite(LPWM_Output, 0);
  analogWrite(RPWM_Output, reversePWM);
}
else
{
  // forward rotation
  int forwardPWM = (sensorValue - 512) / 2;
  analogWrite(LPWM_Output, forwardPWM);
  analogWrite(RPWM_Output, 0);
}

Here's your onInterrupt function:

void onInterrupt()
{
  // read both inputs
  int a = digitalRead(ENCODER0PINA);
  int b = digitalRead(ENCODER0PINB);

  if (a == b )

    // b is leading a (counter-clockwise)
    encoder0Position--;
  currentDirection = COUNTER_CLOCKWISE;
}

You can see it starts with a { and ends with the matching }.

It's followed by all this code:

else
{
  // a is leading b (clockwise)
  encoder0Position++;
  currentDirection = CLOCKWISE;
}

// track 0 to 20000
{
  encoder0Position = encoder0Position % CPR;
}

// track the number of interrupts
interruptsReceived++;
//read the pushbutton value into a variable

{
  int sensorVal = digitalRead(36);
  //print out the value of the pushbutton
  Serial.println(sensorVal);


  int sensorValue = analogRead(SENSOR_PIN);

  // sensor value is in the range 0 to 1023
  // the lower half of it we use for reverse rotation; the upper half for forward rotation
  if (sensorValue < 512)

    // reverse rotation
    int reversePWM = -(sensorValue - 511) / 2;
  analogWrite(LPWM_Output, 0);
  analogWrite(RPWM_Output, reversePWM);
}
else
{
  // forward rotation
  int forwardPWM = (sensorValue - 512) / 2;
  analogWrite(LPWM_Output, forwardPWM);
  analogWrite(RPWM_Output, 0);
}

Even without looking at braces, you can see from the indentation that code is outside of any function. You can't have code outside of a function. Clearly, that code was intended to be part of the onInterrupt function.

So what happened? You forgot to add an opening brace on this line:

 if (a == b )

Again, the indentation is trying to tell you something if you'll only look:

 if (a == b )

    // b is leading a (counter-clockwise)
    encoder0Position--;
  currentDirection = COUNTER_CLOCKWISE;

Notice how this line is indented past the if:

   encoder0Position--;

but the next line is not:

 currentDirection = COUNTER_CLOCKWISE;

That is because when you don't use braces after an if, the if only affects the first statement following. But that's not what you intended. You intended for the if to control all the code up to the else.

I gave you some advice earlier but you ignored it:

pert:

bmachining:

  if (digitalRead (36) == LOW)

Serial.println ("Switch closed.");

Always use braces, even if they only contain one statement. It's worth that tiny bit of extra effort to make your code more self documenting and resistant to bugs.

Yes, you can get away without the braces if there is only one statement, but what if you decide to add another line later? It's just not worth it. Always use the braces. If you had done so in this case it would have saved a lot of confusion and wasted time.

Things become even a more confusing because you added that unnecessary blank line, which I already told you not to do. Every line, every character, in your program should have a purpose. The compiler has no mercy. It's not going to say "yo, it's all good, I know what you meant". This is why you need to take an extremely precise approach to writing code.

This might help you understand what the braces are for and why it's so important that they're all correct:
https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/
Do keep in mind that Arduino sketches are written in standard C++. You don't need to restrict your research to Arduino-specific information. The Arduino reference does not attempt to be a comprehensive C++ reference. There are already many such references that do a much better job than Arduino could do. There is a tremendous amount of information available to teach you how to write C++. You do need to understand that writing programs that run on a computer is different in some ways than writing code that runs on a microcontroller such as the ones on your Arduino board. The computer has much more resources. So you do need to be able to gauge which parts of general C++ information is not relevant to Arduino. With a little practice it's easy enough to do.

Thankyou pert. Some examples and an explanation go a long way.
The indentation is something i didnt realize was actually telling me something.

I see some examples have { then another { followed by }} does this mean that something is looked at separately or is it just complicating things?

bmachining:
The indentation is something i didnt realize was actually telling me something.

I told you that in reply #4, then again in #18 and #24.

bmachining:
I see some examples have { then another { followed by }} does this mean that something is looked at separately or is it just complicating things?

I'd need to see the example to give a specific answer. Each set of braces defines a scope. Some code might have many nested braces, each with their own purpose. Consider this code snippet:

void loop()
{ //start the loop() scope
  if (something == true)
  { //start the if scope
    for (byte x = 0; x < 42; x++)
    { //start the for scope
      if (x == 22)
      { //start the if scope
        foo();
        asdf = false;
      } //end the if scope
      bar();
    } //end the for scope
  } //end the if scope
} //end the loop() scope

Do you see how confusing that could be without proper formatting? And that's even a very simple program! With consistent formatting, there is no need to waste time adding the silly comments like in the example because you can already see that with a brief glance at the code. Your eyes can jump right to the section you're looking for without needing to read it line by line. This is why we always harp on people to format their code before posting it on the forum. I often will have a good idea of where to look for an error and I don't want to have to read a bunch of irrelevant code to get to it.

OK i still have errors but seemed to format ok. Hope its an improvment!

#define ENCODER0PINA         20      // this pin needs to support interrupts
#define ENCODER0PINB         18      // interuppt
#define CPR                  400     // encoder cycles per revolution
#define CLOCKWISE            1       // direction constant
#define COUNTER_CLOCKWISE    2       // direction constant

volatile long encoder0Position = 0; // variables modified by interrupt handler must be declared as volatile
volatile long interruptsReceived = 0;
short currentDirection = CLOCKWISE;// track direction: 0 = counter-clockwise; 1 = clockwise
long previousPosition = 0;// track last position to see if worth printing new output
int SENSOR_PIN = 0; // center pin of the potentiometer
int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)

void setup()
{
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  pinMode(RPWM_Output, OUTPUT);
  pinMode(LPWM_Output, OUTPUT);
  pinMode(36, INPUT_PULLUP); //home switch

  // inputs
  pinMode(ENCODER0PINA, INPUT_PULLUP);
  pinMode(ENCODER0PINB, INPUT_PULLUP);// interrupts
  attachInterrupt(3, onInterrupt, RISING);
  Serial.begin (9600);        // enable diagnostic output
  Serial.println("\n\n\n");
  Serial.println("Ready.")
  analogWrite(LPWM_Output, 100);      //drive to home switch
  if (digitalRead (36) == LOW)
    Serial.println ("Switch closed.");
  delay (1000);
  analogWrite(RPWM_Output, 100);
}

void loop()
{
  if (encoder0Position != previousPosition)
    Serial.print(encoder0Position, DEC);
  Serial.print("\t");
  Serial.print(currentDirection == CLOCKWISE ? "clockwise" : "counter-clockwise");
  Serial.print("\t");
  Serial.println(interruptsReceived, DEC);
  previousPosition = encoder0Position;
  void onInterrupt()    // interrupt function needs to do as little as possible
  int a = digitalRead(ENCODER0PINA);
  int b = digitalRead(ENCODER0PINB);  // read both inputs
  if (a == b )         // b is leading a (counter-clockwise)
    encoder0Position--;
  currentDirection = COUNTER_CLOCKWISE;
  else
    encoder0Position++;      // a is leading b (clockwise)
  currentDirection = CLOCKWISE;
  encoder0Position = encoder0Position % CPR;   // track 0 to 20000
  interruptsReceived++;        // track the number of interrupts
  int sensorVal = digitalRead(36); //read the pushbutton value into a variable
  Serial.println(sensorVal);   //print out the value of the pushbutton
}
{
  int sensorValue = analogRead(SENSOR_PIN);
  // sensor value is in the range 0 to 1023
  // the lower half of it we use for reverse rotation; the upper half for forward rotation
  if (sensorValue < 512)
  }
{
  // reverse rotation
  int reversePWM = -(sensorValue - 511) / 2;
  analogWrite(LPWM_Output, 0);
  analogWrite(RPWM_Output, reversePWM);
  else
    // forward rotation
    int forwardPWM = (sensorValue - 512) / 2;
  analogWrite(LPWM_Output, forwardPWM);
  analogWrite(RPWM_Output, 0);
}

bmachining:
OK i still have errors

Post them - in code tags.

Arduino: 1.8.7 (Windows 8.1), TD: 1.44, Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\HP\Documents\Arduino\formatted\formatted.ino: In function 'void setup()':

formatted:29:22: error: 'onInterrupt' was not declared in this scope

attachInterrupt(3, onInterrupt, RISING);

^

formatted:33:3: error: expected ';' before 'analogWrite'

analogWrite(LPWM_Output, 100); //drive to home switch

^

C:\Users\HP\Documents\Arduino\formatted\formatted.ino: In function 'void loop()':

formatted:50:3: error: expected initializer before 'int'

int a = digitalRead(ENCODER0PINA);

^

formatted:52:7: error: 'a' was not declared in this scope

if (a == b ) // b is leading a (counter-clockwise)

^

formatted:55:3: error: 'else' without a previous 'if'

else

^

C:\Users\HP\Documents\Arduino\formatted\formatted.ino: At global scope:

formatted:63:1: error: expected unqualified-id before '{' token

{

^

formatted:69:1: error: expected unqualified-id before '{' token

{

^

exit status 1
'onInterrupt' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

For the third, and last, time, I'll repeat myself:

I gave you some advice earlier but you ignored it:

pert:

bmachining:

  if (digitalRead (36) == LOW)

Serial.println ("Switch closed.");

Always use braces, even if they only contain one statement. It's worth that tiny bit of extra effort to make your code more self documenting and resistant to bugs.

Yes, you can get away without the braces if there is only one statement, but what if you decide to add another line later? It's just not worth it. Always use the braces. If you had done so in this case it would have saved a lot of confusion and wasted time.

We're doing our best to help you here but you just seem to ignore 90% of what we say. Why should I bother spending the time on this if you're just going to ignore me?

Im not ignoring any of it. Knowing what does need braces and what doesn't is not that straight forward.

So everything needs to be in a { } but when are ( ) required or not?

@pert - If we give him a solution, do you think he might study it and learn?

bmachining:
Im not ignoring any of it.

If there's something you don't understand, you need to ask. If I tell you to do something and you don't do it or ask for clarification then I consider that to be ignoring what I said, regardless of your reasons.

bmachining:
Knowing what does need braces and what doesn't is not that straight forward.

It is when I specifically tell you that every if should be followed by braces. The same goes for else and else if. I didn't say that before because you actually had those braces. But now you removed them!

When in doubt, you only need to do a little research:

bmachining:
when are ( ) required or not?

You use parentheses to enclose the arguments passed to a function:

someFunction(42);

and to enclose the parameters in a function definition:

void someFunction(int someParameter)
{
  // do something with someParameter
}

DKWatson:
@pert - If we give him a solution, do you think he might study it and learn?

I don't think so. There are already plenty of examples available to study and learn. Certainly, providing a working program would have been much faster and easier for us, but my goal here is to help people learn, not to provide a free code writing service.