atoi()

hello to all,
I'm trying to read data from serial
I would like to use these data to make operations with the types INT.
I wrote this sketch:

char t[3];
int a=0;
int b=0;
void setup()
{
  Serial.begin(9600); 
 


}
void loop()
{   
  if (Serial.available()>0)
  {
    for (int i=0;i<2;i++){
      if (Serial.available()>0){
        t[i]=Serial.read();
        t[2]=0;
        a++;

      }
    }
    if (a>0){

      Serial.print(t[0]);
      Serial.print(t[1]);
      Serial.print(t[2]);
      b++;
      a=0;
    }
    delay (2000);
    if (b>0){
      int c= atoi(t);//convert t[] to int c
      c=c+2;
      Serial.print(c);
      b=0;
    }
  }

}

but if I write for example 58, the result is:

5   78  10

the addition was
5 +2 = 7
8 +2 = 10
and not 58+2=60
and does not write the first values ??of v [] and after the value of c, it switches between them!
Why????
sorry for my english :stuck_out_tongue:

    for (int i=0;i<2;i++){
      if (Serial.available()>0){
        t[i]=Serial.read();
        t[2]=0;
        a++;

      }
    }

Rubbish. NEVER use if(Serial.available()) in a for loop like this.

Fix that, and see if it affects your problem.

Also, the NULL should be in t[i+1], not t[2].

if I delete the control Serial.available () from the cycle I have read errors (5ÿ8ÿ)
I changed t [i +1], but the result does not change

if I delete the control Serial.available () from the cycle I have read errors (5ÿ8ÿ)

Think about what your "for" loop does in the code you posted first, if there's no data available.

(FYI "ÿ" == -1 )

This is not how to do it.

for (int i=0;i<2;i++){
      if (Serial.available()>0){
        t[i]=Serial.read();
        t[2]=0;
        a++;

      }

Do you just want 2 chars or multiple?

t[0] = Serial.read();
t[1] = Serial.read();

or

t[i] = Serial.read();
i++;

use an IF or FOR loop to limit the incoming chars.

ok I'm stupid!!
an error in}
the for loop is closed at the end of loop () and not immediately after reading the characters from the serial
Thanks to all and sorry!