[Solved] need help in Char array that auto grow


You are not null-terminating the arrays you create. Try this instead:

char* dynamicmem(char size) {
    char* temp = new char[size + 1];
    temp[size] = '\0';
    return temp;
}

char* regrow(char* ptr, int size, char c) {
    char *p = dynamicmem(size + 1);
    for (int i = 0; i < size; i++) {
        p[i] = ptr[i];
    }
    p[size] = c;
    delete [] ptr;
    return p;
}
    
int main() {
    int size = 0;
    char *name = dynamicmem(size);
    cout << "Enter Name: ";
    char c;
    while (cin.get(c) && c != '\r' && c != '\n') {
        name = regrow(name, size, c);
        ++size;
    }
    cout << endl << name;
    delete[] name;
}

0

solved need help in Char array that auto grow