Adding Characters to String Causes Errors

So I have a project where a 433 Mhz radio link receives a signal from another Arduino. I have functions for interpreting the code sent depending on the situation. I store these codes into the Arduino with EEPROM and to decipher when one code ends and another starts, I put "Y" at the beginning and "Z" at the end. Here is where the problem is, in this snippet, I am simply trying to add "Y" to the beginning and "Z" to the end but in the serial monitor, 'finalvar' prints "Z". This makes no sense. 'rcv' prints normally("C7FE1119" for example) but is not added properly.

    Serial.println(rcv);
    String finalvar = "Y" + rcv + "Z";
    Serial.println(finalvar);

As requested, here is the entire program:

#include <EEPROM.h>
#include <SPI.h>
#include <RH_ASK.h>
RH_ASK driver(2000, 2, 4, 9);
int j = 0;
String code = "";
int Card = -1;
String List[100] = {};
int lengthv = 0;
bool reading = false;
String rcv = "";
void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  if (!driver.init())
    Serial.println("init failed");
  pinMode(5, INPUT);
  pinMode(8, INPUT);
  pinMode(3, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  for (int i = 0; i < EEPROM.length(); i++) {

    j = EEPROM.read(i);
    Serial.println(j);

    if (char(j) == 89 ) {
      Serial.println("Reading True");
      Card = Card + 1;
      Serial.println("Card:");
      Serial.println(Card);
      reading = true;
    }
    if (char(j) == 90) {
      Serial.println("Reading False");
      reading = false;
      List[Card] = code;
      code = "";
    }
    if (j == 255) {
      Serial.println("Length:");
      Serial.println(lengthv);
      break;
    }
    lengthv ++;

    if (reading == true) {
      if (char(j) != 89 && char(j) != 90 && char(j) != 0) {
        char middle = char(j);
        code += middle;
      }
    }
  }
  Serial.println(List[0]);
  Serial.println(List[1]);
  Serial.println(List[2]);
  Serial.println(List[3]);
  Serial.println(List[4]);
  Serial.println(List[5]);
  Serial.println(lengthv);
}
int reset = 0;
bool programming = false;
bool add = false;
void loop() {
  delay(50);
  int value = digitalRead(8);
  int value2 = digitalRead(5);

  if (programming == true) {
    reset = reset + 1;
  }
  if (reset == 50) {
    reset = 0;
    Serial.println("Programming False");
    programming = false;
    digitalWrite(7, LOW);
    digitalWrite(3, LOW);
  }
  if (value == 1) {
    reset = 0;
    Serial.println("Card Time");
    programming = true;
    digitalWrite(3, HIGH);
    digitalWrite(7, LOW);
    add = true;
  }
  if (value2 == 1) {
    reset = 0;
    Serial.println("Delete Time");
    digitalWrite(7, HIGH);
    digitalWrite(3, LOW);
    add = false;
    programming = true;
  }
  rcv = "";
  //Recieves the Card Number
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);
  if (driver.recv(buf, &buflen)) // Non-blocking
  {    
    for (int i = 0; i < buflen; i++) {      
      rcv += (char)buf[i];
    }   
    Serial.println(rcv);
  }else{
    return;
  }
  if (programming == true && rcv.length() > 0 ) {
    if (add = true) {
      Serial.println(rcv);
      AddCard();
    } else {
      SubCard();
    }
  } else {
    for (int i = 0; i < 5; i++) {
      if (rcv == List[i] && rcv.length() > 0) {

        digitalWrite(6, HIGH);
        delay(600);
        digitalWrite(6, LOW);
      }
    }
  }
}

void AddCard() {
  bool match = false;
  Serial.println("Adding");
  Serial.println(rcv);
  for (int x = 0; x < 5; x++) {
    if (rcv == List[x]) {
      match = true;
      Serial.println("A match!");
    }
  }

  if (match == false) {
    Serial.println("False Match");
    Serial.println(rcv);
    String finalvar = "Y" + rcv + "Z";
    Serial.println(finalvar);
    Card ++;
    List[Card] = rcv;
    
    
    for (int s = 0; s < finalvar.length(); s++) {
      Serial.println("Snoitch");
      char w = finalvar[int(s)];
      Serial.println(w);
      Serial.println(lengthv);
      EEPROM.write(lengthv, w);
      lengthv ++;
    }
    flicker(3);
  }
}

void SubCard() {
  bool match = false;
  int chosen = 0;
  String MegaString = "";
  for (int x = 0; x < 100; x++) {
    if (rcv == List[x]) {
      match = true;
      chosen = x;
    }
  }
  if (match == true) {
    List[chosen] = "";
    for (int x = 0; x < 100; x++) {

      if (x != chosen) {
        String placeholder = "Y" + List[x] + "Z";
        if (placeholder.length() > 2) {
          MegaString += placeholder;
          Serial.println(placeholder);
          flicker(7);
        }
      }
    }
  }
  Serial.println(MegaString);

  for (int o = 0; o < EEPROM.length(); o++) {
    EEPROM.write(o, 0);
  }
  for (int i = 0; i < MegaString.length(); i++) {
    EEPROM.write(i, MegaString[i]);
  }

}
void flicker(int pin) {
  programming = false;
  digitalWrite(pin, LOW);
  delay(150);
  digitalWrite(pin, HIGH);
  delay(150);
  digitalWrite(pin, LOW);
  delay(150);
  digitalWrite(pin, HIGH);
  delay(150);
  digitalWrite(pin, LOW);
}

Why is the string not adding properly?

A code snippet is not enough to diagnose the problem.

Please post a small but complete example sketch that illustrates the problem.

I added the sketch into the question, thanks for the help!

try    String finalvar = String("Y") + rcv + "Z";or    String finalvar = String("Y") + rcv + String("Z");

I tried both but I'm still having the same problem. It also might be worth noting that if I clear the EEPROM, the first card works, but any card after that does not.

What Arduino are you using ?

There is at least one bug here if (add = true) {(this is an assignment)

The code is super spaghetti so hard to follow what you are really doing from reading on my smart phone.

You should question The heavy use of String class And the List array and double check all fits in memory.

If you add and delete Strings in random order you end up poking holes in the heap And memory allocation for new strings can fail (which you don’t test for).

What’s up with reading the full eeprom at start ? What do you store there ?

Comparing char does not require calling char(variable) just do if (variable == ‘J’) {...}

My advice would be

  • get rid of the string class and stick to fixed size cStrings. That will give you a good grip On memory usage
    (Use strcmp() to compare two cStrings, and strcpy() to copy - ensuring the length is suitable )

  • reorganize the code to make the flip clearer. Use a state machine approach

  • and fix the = versus == bug

Thanks for all the help, I'l try some of your suggestions and get back to you. The reason why I read the EEPROM at the start is to create the list of cards that the arduino is trying to match with. Also, I am using the Arduino Nano 328P (Old bootloader).