problem using while

this is my code:

void setup() {

Serial.begin(9600);

}

void loop() {

Serial.println("Enter number: ");
while (Serial.available()==0);
int val=Serial.parseInt();
Serial.println(val);

}

when I execute the Sketch and input 5 I expect this:

Enter number:
5
Enter number:

But what I get is this:

Enter number:
5
Enter number:
0
Enter number:

What am I doing wrong?

Have a look at this example of how ‘serialavailable’ works,

https://www.arduino.cc/reference/en/language/functions/communication/serial/available?from=Reference.SerialAvailable

You are basically only looking for data when there is none ; If you use : while(Serial.available()==0){ }

Then it will wait in this loop whilst no data is available, and when there is some execute the code that follows .

@hammy, that would not make a difference as far as I know; there is a semi-colon after the while.

@mac-7, what is the line-ending in serial monitor set to? Set it to 'none' if it isn't.

@mac-7

Moved your topic to it’s current location / section as it is more suitable.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.