You need to initialise your variables correctly:
int firstLength = str1.length();
int secondLength = str2.length();
You’re also determining that one string is bigger than another, then proceeding to access elements in the shorter string according to the length of the longer one, which is giving you the subscript out of range error. You need to revise the tests when firstLength > secondLength and vice versa.
3
solved C++ String subcript out of range issue