I want to add elements in the array of class MyString like this
void print(const MyString &obj)
{
obj.print();
}
int main()
{
MyString str1; // make a default string of 100 size
str1.add('[') ;//insert at position 1 or index 0
str1.add('A'); //insert at position 2 or index 1
str1.add('B'); //insert at position 3 or index 2
str1.add('C'); //insert at position 4 or index 3
str1.add('D'); //insert at position 5 or index 4
str1.add('E'); //insert at position 6 or index 5
str1.add('F'); //insert at position 7 or index 6
str1.add('G'); //insert at position 8 or index 7
str1.add('h'); //insert at position 9 or index 8
str1.add('i'); //insert at position 10 or index 9
str1.add('j'); //insert at position 11 or index 10
str1.add('k'); //insert at position 12 or index 11
str1.add('l'); //insert at position 13 or index 12
str1.add('m'); //insert at position 14 or index 13
str1.add('n'); //insert at position 15 or index 14
str1.add('o'); //insert at position 16 or index 15
str1.add('p'); //insert at position 17 or index 16
str1.add(']'); //insert at position 18 or index 17
print(str1);
}
Wrote a function which would take value as a parameter and insert it in the array
void MyString::add(char value)
{
char * temp = new char[strlen(this->arr) + 1];
for (int i = 0; i < strlen(this->arr); i++)
{
temp[i] = this->arr[i];
}
temp[strlen(this->arr)] = value;
delete[]this->arr;
this->arr = temp;
temp = nullptr;
}
and a print function to display the elements in array
void MyString::print() const
{
cout << this->arr << endl;
}
But the code doesn't display anything during debugging.. Where am I getting it wrong?