Serial to Parallel Shifting-Out with a 74HC595 - Example issue

Hello,
I've tried to implement the program found at this page http://arduino.cc/en/Tutorial/ShftOut12.
It makes an example on how to use a shift register (circuit connections are here http://www.arduino.cc/en/Tutorial/ShiftOut).

In such an example, when I send my bit/pin selection via the serial line through the IDE Monitor interface, the micro runs the loop twice (thus it processes the "shifOut" command two times) even if I send my input just once.
The first time it indeed processes exactly my bit selection (a number from 0 to 7), the second time it processes a "carriage return" character.

This implies that, on the second time, the variable "pinToSet" becomes -35 (since the ASCII value for the carriage return is "13"). Finally, at the end of the second shiftOut, only the least significant bit is set HIGH.

Practically: whatever LED you choose to light up, you get it flickering only, but then the program freezes the register value upon the second loop closure and you always get your first LED permanently turned on (not the once you chose).

In order to fix the issue, I've therefore added just an additional "if" after the one already present in the program, so that the loop becomes:

void loop() {
  if (Serial.available() > 0) {  
    int bitToSet = Serial.read() - 48;                                        
    if (bitToSet < 0) { 
      bitToSet = prevBitToSet;
    }
    else {
       prevBitToSet = bitToSet; 
    }  
  }
    registerWrite(bitToSet, HIGH);
}

So when the program processes the "carriage return" which apparently is always generated, I restore the value I chose with my selection.
Let me know if you also experienced such a behavior or if the additonal "carriage return" character is added by default by the IDE Serial Monitor, so it must indeed be filtered out.

Thanks for your feedback!

So when the program processes the "carriage return" which apparently is always generated, I restore the value I chose with my selection.

Always generated by what? If you are using the Serial Monitor application to send the data, the carriage return (and line feed) is always generated only if you choose that option. If you don't want that, and can't figure out how to ignore it (trivial), don't pick that option.

What have you got the serial monitor line ending set to ?
No line ending ?
Newline ?
Carriage return ?
Both NL & CR ?