Serial Port Doesn't Work As Expected

I'm trying to read a number from the serial port, but every time I enter a number, the Arduino reads the number and reads 0 after that. Can someone help me, please? (I'm using the Arduino Uno R3)

int myNumber;
String msg1 = "Please enter your number";
String msg2 = "Your number is ";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(msg1);
  while(Serial.available()==0){
    
  }
  myNumber = Serial.parseInt();
  Serial.print(msg2);
  Serial.println(myNumber);
  delay(1000);
}[code]

every time I enter a number, the Arduino reads the number and reads 0 after that.

What have you got the Line ending set to in the Serial monitor ?

There's a png to download at the bottom of the post.

Abran:
There's a png to download at the bottom of the post.

My question was intended as a clue to you as to what might be causing the problem

What do you suppose gets sent after the number that you enter ?

A blank number?

(deleted)

Abran:
A blank number?

Why would anyone be so perverse?

With Serial.parseInt() you have two choices:

  1. Set 'line ending' to 'none' and let the .parseInt() time out (default 1 second).
    Or:
  2. Set 'line ending' to 'newline' or some other setting that adds a non-digit at the end of the string.

Choice 2 is more responsive, since you don't have to wait for the timeout, but then you have to deal with the extra character.

This method works either way:

void loop() {
  // put your main code here, to run repeatedly:
  while (Serial.available()) Serial.read();  // Empty the input buffer
  Serial.println(msg1);  // Prompt for input
  while(Serial.available() < 1){} // Wait until at least one character is available
  myNumber = Serial.parseInt();  // Read the number until timeout or non-digit
  Serial.print(msg2);
  Serial.println(myNumber);
  delay(1000);
}

I changed the serial monitor from Newline to No line ending, and it worked. Thanks, everyone!