Serial read loop not reading \n

I am new to Arduino and C. I have writeen a sketch to check, whether a year is a leap year.
In order to allow user input I have added a function to read the serial monitor, like so:

int ReadLine(char str[]) {
  char c;
  int index = 0;
  while (true) {
    if (Serial.available() > 0) {
      c =;
      Serial.print("Received char: ");
      Serial.println(c, HEX);
      if (c != '\n') {
        Serial.print("Debug (ReadLine | if > char): ");
        str[index++] = c;
      } else {
        Serial.print("Debug (ReadLine | if > else): ");
        str[index] = '\0';       // null termination character
      Serial.print("Debug (ReadLine | str): ");
      Serial.print("Debug (ReadLine | index): ");
  return index;

The problem is that I never reach the ‘else’ part when checking for the new line character.
Entering: 2012, I expect to see with Serial.println(c, HEX) the output as: 32 30 31 32 0D 0A, with oD being the \n terminator.

I have not added input validation or other fancy stuff (and am aware the sketch loops forever to read the serial port), I simply wanted to understand to build bigger projects.

What am I doing wrong in this sketch?

Any help appreciated.

IIRC the IDE never sends an '\n' as it is the trigger to know when to send.

you could try to replace the \n with # instead and see if that works...

Thanks, this was quick…

Changing the condition to use ‘#’ or ‘x’ does produce a different result; it will enter the ‘else’ part. Great.

However, is there a way to use the key as line terminator for this function?
Why is

      Serial.print("Received char: ");
      Serial.println(c, HEX);

not repeating the or more so the hex 0D (\n) character?

Re-reading your answer: what is it sending then?
I can enter one or two or more numbers, and nothing happens. But once I hit the input is read.

Can't you just change the line ending option on the serial monitor?

@AWOL: Hmm, I am using the IDE 1.0.5 and can't seem to find this option in the preferences or elsewhere.
Where can I change this?

My previous question still stands: why is Serial.Read() not reading the '\n\ or maybe Serial.print() can't display it?

It's in the serial monitor.

Ouch... this hurts!!
Thanks AWOL! This is most embarrassing...
This ends three hours of pulling my hair out :astonished: