Serial input (presumably) reading newline, how can I fix this?

I am just working on a lab for school, but am encountering an issue that has caused me problems for years in programming, and I just want to squash this once and for all. I am writing example code that reads:

void loop()
{

}

Can you tell me what's wrong with this picture:

I am just working on a lab for school, but am encountering an issue that has caused me problems for years in programming, and I just want to squash this once and for all. I am writing example code that reads:

void loop()
{
  char input;
  if(Serial.available() > 0)
  {
     input = Serial.read();
     if(input >= '0' && input <= '9')
    {
       Serial.print("Number received: ");
       Serial.println(input);
    }
    else
    {
       Serial.println("Not a number.");
    }
  }
}

This code was copied directly from an example on YouTube, but for me reads:

//Assuming I entered an 8, for example
Number received: 8
Not a number.
//It then waits - I enter a 7
Number received: 7
Not a number.

I am assuming it is reading the newline character. How can I fix this?

EDIT: I experimented by just hitting the “send” button instead of enter, but it is still giving the same message.

In your serial monitor select “No Line Ending” and/or insert the following in to your if statement block:

    else if(input == '\r' || input == '\n')
    {
       //do nothing - ignore CR and NL
    }

Why on earth do you care? If you only want to parse digits, the just throw anything else away. Or, specifically filter out CR (0x0d or '\r') and LF/NL (0x0a or '\n'). Then you don't care whether they are sent or not.

Regards,
Ray L.

@BigDaddyBlash, please do not cross-post. Threads merged.

Power_Broker:
In your serial monitor select "No Line Ending" and/or insert the following in to your if statement block:

    else if(input == '\r' || input == '\n')

{
      //do nothing - ignore CR and NL
   }

You could, for testing purposes, to validate the idea that those characters are there, enhance P_B's code to print:

    else if(input == '\r' || input == '\n')
    {
       Serial.println("Yep, got a CR or LF there alright");
    }

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R