Problem in Serial.Available() using for Password and Clearing it

Hope you guys help me..
every time i input the expected output is correct but theirs an extra... i'm thinking of clearing Serial.Available every time a condition was done for this problem but i dont know how. i tried using Serial.flush() but its not working.

example of that extra output is when i input "abcd"(which is the correct passwrord) and verified that its correct the extra out is driven even if i only input 1 byte this how it looks like in serial monitor:

First inputed byte :
a
Matching
a
First Byte Correct

Second inputed byte :
b
Matching
b
Secont Byte Correct

Third inputed byte :
c
Matching
c
Third Byte Correct

Forth input byted :
d
Matching
d
Forth Byte Correct
CORRECT PASSWORD

First inputed byte :
Matching
a
First Byte Wrong

The BOLD words is the extra output even w/o Serial.read() it runs
below is my code you can try it~

please need help :frowning:

char inputpw[4];
char correct[4] = {'a','b','c','d'}; //User-Defined Password
int bytes=0;
int correct_byte=0;
int tenge=0;

void setup()
{

Serial.begin(9600);

}

void loop()
{
while (Serial.available()>0)

{

if(bytes==0 && Serial.available()>0)
{
inputpw[bytes]=Serial.read();
Serial.println("First inputed byte :");
Serial.println(inputpw[bytes]);
Serial.println("Matching");
Serial.println(correct[bytes]);

if(inputpw[bytes]==correct[bytes])
{
Serial.println("First Byte Correct ");
Serial.println();
bytes++;
correct_byte++;
}
else
{
Serial.println("First Byte Wrong");
Serial.println();
bytes++;
tenge=1;
}
}

else if(bytes==1 && Serial.available()>0)
{
inputpw[bytes]=Serial.read();
Serial.println("Second inputed byte :");
Serial.println(inputpw[bytes]);
Serial.println("Matching");
Serial.println(correct[bytes]);

if(inputpw[bytes]==correct[bytes])
{
Serial.println("Secont Byte Correct");
Serial.println();
bytes++;
correct_byte++;
}
else
{
Serial.println("Second Byte Wrong");
Serial.println();
bytes++;
tenge=1;
}
}

else if(bytes==2 && Serial.available()>0)
{
inputpw[bytes]=Serial.read();
Serial.println("Third inputed byte :");
Serial.println(inputpw[bytes]);
Serial.println("Matching");
Serial.println(correct[bytes]);

if(inputpw[bytes]==correct[bytes])
{
Serial.println("Third Byte Correct");
Serial.println();
bytes++;
correct_byte++;
}
else
{
Serial.println("Third Byte Wrong");
Serial.println();
bytes++;
tenge=1;
}
}

else if(bytes==3 && Serial.available()>0)
{
inputpw[bytes]=Serial.read();
Serial.println("Forth input byted :");
Serial.println(inputpw[bytes]);
Serial.println("Matching");
Serial.println(correct[bytes]);

if(inputpw[bytes]==correct[bytes])
{
Serial.println("Forth Byte Correct");
bytes++;
correct_byte++;

}
else
{
Serial.println("Forth Byte Wrong");
bytes++;
tenge=1;
}
}

if(correct_byte==4)
{
Serial.println("CORRECT PASSWORD");
Serial.println();
/* delay(500);
digitalWrite(lock, HIGH);
delay(3000);
digitalWrite(lock,LOW);*/
bytes=0;
correct_byte=0;

}
else if(tenge==1 && bytes==4)
{
Serial.println("WRONG PASSWORD");
Serial.println();
bytes=0;
tenge=0;
}

}

}

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

Tl,Dr.

Code tags. They're the future

Useful stuff here

while (Serial.available()>0)
     
        {
         
     
               
               if(bytes==0 && Serial.available()>0)
                  {

How do you suppose it's going to (eventually get past all the stupid white space and) get to the if statement if there isn't serial data to read?

You need to send your data between start- and end-markers so that the receiving code ignores everything before the start-marker and then saves everything until it detects the end-marker. You can use any convenient byte values for the markers as long as they never appear in the data.

The demo code in this thread may be of use Demo of PC-Arduino comms using Python - Interfacing w/ Software on the Computer - Arduino Forum
The demo uses byte values of 254 and 255 for the markers.

...R