unable to get first character in a character string

Hi all!
I am using arduino due and i wrote a small program to read characters and write them into the string. afterwards when i try to display them , first entry is always missing . can anyone explain this to me?

Thanking you in advance

With regards
Saqib

My code is given below

char inputarray[7] ;

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

 while (Serial.read()>0)
{
 
  for (int i=0;i<6;i++)
  {
     char input;
     input = Serial.read();
    inputarray[i]= input;
  
 }
    
}
 
 for (int i=0; i<6; i++)
{ 
  Serial.println(inputarray[i]);

}

}

You’re discarding the first character

Use “.available()”, and take a look at the serial handling basics topic.

  while (Serial.read() > 0)

If there is a character to read then Serial.read() will read it and return a number greater than 0 but you throw away what is read, hence the missing first character

Why not use Serial.available(), which does not read a character, to test whether there is anything to read ?

NOTE - either test for something being available before each read or test that the whole string is available to read before reading anything

This is the usual way of writing sketch for serial communication port and not the way you have written. Upload the following sketch with 'line ending tab' of Serial Monitor (Fig-1) at 'Newline' option.

char inputarray[7] ; //tested with string Forum
int i = 0;

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

void loop()
{
  // put your main code here, to run repeatedly:
  /*
    while Serial.available()>0)//(Serial.read() > 0)
    {

      for (int i = 0; i < 6; i++)
      {
        char input;
        input = Serial.read();
        inputarray[i] = input;
      }
    }
  */

  byte n = Serial.available(); //check if character has arrived
  if (n != 0)  //charcater has arrived and entered into FIFO Buffer
  {
    char x = Serial.read();   //get character from FIFO Buffer
    if (x != '\n')  //no newline character is received
    {
      inputarray[i] = x;  //save character in user array
      i++;   //adjust array index to hold next character
    }
    else //newline charcater (\n = 0x0A) has arrived from Serial Monitor indicating the end of string sent
    {
      inputarray[i] = '\0';  //insert null-character at end of received string; it is needed for print purpose
      i = 0;
      Serial.println(inputarray);  //print the received string.
    }
  }
  /*
    for (int i = 0; i < 6; i++)
    {
      Serial.println(inputarray[i]);

    }
  */
}

SerialMonitor.png
Fig-1:

SerialMonitor.png

I see you still haven’t got the hang of null-terminated strings, @GolamMostafa.

TheMemberFormerlyKnownAsAWOL:
I see you still haven't got the hang of null-terminated strings, @GolamMostafa.

Right Sir; I have made it corrected (+).

The reason i asked this question and took valueable time of arduino forum members is that i wrote a similar code for arduino uno and it was working as a i anticipated but if run the same code on arduino due it skips first character. If the code is same then the output should be same. should it?

The code may have been the same but were the Line ending settings in the Serial monitor the same ?