atoi Issue when using CircularBuffer Library

I am building a sketch for an UNO that will eventually talk to a display and use a keypad, but I’m developing it using Serial Terminal for now. It is an initiative tracker for Dungeons and Dragons. I am new at this, and not a super experienced programmer, so bear with me.

When I implemented the CircularBuffer, I get a weird result in my setPCinitiative function. I’ve tried using step by step debugging, and I found out what is going wrong, but not how to fix it. An example of what its doing is when the user enters “01@19” it should split the input into the intagers 1 and 19, it should set the initRoll to 19 for character 01. However the code is assigning 1901 to initRoll.

The previous version did work find, before I implemented the CircularBuffer Library, and I didn’t change any code in this function.

Here is just the setPcInitiative function by itself:

void setPCinitiative (const char * data){
  char pcMonster[2] = {data[0],data[1]};
  int pcMonsterInt = atoi(pcMonster);
  char pcInit[2] = {data[3],data[4]};
  int pcInitInt = atoi(pcInit);
  tomb[pcMonsterInt].initRoll = pcInitInt;
  tomb[pcMonsterInt].engage();
}

Here is the full code:

#include <CircularBuffer.h>
#include <Monster.h>
//Monster(char* mName,bool inCombat, int initRoll,int hp, int initMod)
//.roll() updates initRoll with D20+initMod
//.engage() move to combat
//.kill() remove from combat



//----------------------------------------------------------------------------------------------
//Variables
const unsigned int MAX_INPUT = 10;
const int TOMB_LENGTH = 11;     //the length of the master tomb of players and monsters
CircularBuffer<byte,TOMB_LENGTH> turnOrderC;
//int turnOrder[TOMB_LENGTH];    //holds the turn order
int turnPosition = 0;           //holds the turn position
//byte turnCounter = 1;
//byte roundCounter = 1;
Monster tomb[TOMB_LENGTH] = {
  {"vek",18,3,4, false},   //0
  {"bob",21,3,4, false},
  {"jeff",15,3,4, true},
  {"dale",17,3,4, true},   
  {"keith",8,3,4, true},   
  {"evan",23,3,4, true},  //5
  {"goblin1",14,3,4, true},   
  {"goblin2",1,3,4, true},
  {"goblin2",1,3,4, true},
  {"goblin3",1,3,4, true},
  {"goblin4",1,3,4, true}, //10
};





//-----------------------------------------------------------------------------------------------
//functions
void serialPrintEngage(); //Prints out the initiative list in order of initiative
void determineTurnOrder(); //updates array turn order with currently engaged beasts
void nextTurn();

void setPCinitiative (const char * data);
void killMonster (const char * data);

void process_data (const char * data);
void processIncomingByte (const byte inByte);

//----------------------------------------------------------------------------------------------

void setup() {
  Serial.begin(9600);
  Serial.println("Enter number to engage a character");
  Serial.println("Enter cc@ii to engage and set initiative for a PC");
  Serial.println("Enter Kcc to kill off a character");
  Serial.println("Enter G to Get initiative list");
}

void loop() {
   while (Serial.available () > 0)
    processIncomingByte (Serial.read ());
}

void determineTurnOrder(){
  turnOrderC.clear();
  for(int i = 40; i >= 0; i --){
    for(int a = 0; a < TOMB_LENGTH; a++){
      if(tomb[a].initRoll == i && tomb[a].inCombat == true){
        turnOrderC.push(a);
      }
    }
  }
}

void serialPrintEngage() {
  Serial.println("Ordered List");
  for(byte a = 0; a < turnOrderC.size(); a++){
      Serial.print(tomb[turnOrderC[a]].mName);
      Serial.print("\t");
      Serial.print("\t");
      Serial.print(tomb[turnOrderC[a]].initRoll);
      Serial.println("");
  }
}

void process_data (const char * data){
  if(data[0] == 'G'){
    determineTurnOrder();
    serialPrintEngage();    
  }
  else if(data[0] == 'K'){
    killMonster(data);
  }
  else if(data[0] == 'N'){
    nextTurn();
  }
  else if(data[2] == '@'){
    setPCinitiative(data);
  }
  else{
    int dataNumType = atoi(data);
    tomb[dataNumType].engage();
    if(tomb[dataNumType].npc == true){
      tomb[dataNumType].roll();
    }
  }
}  // end of process_data
  
void processIncomingByte (const byte inByte){
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;

  switch (inByte)
    {

    case '\n':   // end of text
      input_line [input_pos] = 0;  // terminating null byte
      
      // terminator reached! process input_line here ...
      process_data (input_line);
      
      // reset buffer for next time
      input_pos = 0;  
      break;

    case '\r':   // discard carriage return
      break;

    default:
      // keep adding if not full ... allow for terminating null byte
      if (input_pos < (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;
      break;

    }  // end of switch
   
  } // end of processIncomingByte

void setPCinitiative (const char * data){
  char pcMonster[2] = {data[0],data[1]};
  int pcMonsterInt = atoi(pcMonster);
  char pcInit[2] = {data[3],data[4]};
  int pcInitInt = atoi(pcInit);
  tomb[pcMonsterInt].initRoll = pcInitInt;
  tomb[pcMonsterInt].engage();
}

void killMonster (const char * data){
  char cMon[2] = {data[1],data[2]};
  int cMonInt = atoi(cMon);
  tomb[cMonInt].kill();
}

void nextTurn(){
  turnOrderC.push(turnOrderC.shift());
  serialPrintEngage();
}

cstring needs to be '\0' terminated.

for example:

char pcMonster[2] = {data[0],data[1]};

should be:

char pcMonster[3] = {data[0],data[1], '\0'};

That fixed it, thank you!