I need to write a program that will read in an unlimited number of names that are an unlimited number of characters long using dynamic memory. Things go smoothly until I get to trying to copy memory from one two-dimensional dynamic array to another.
void readnames () // the first half is for your reference to see what I'm doing
{
constint firstguess (5);
char input;
int numchars;
int arraysize;
int arraysize2;
char * temp;
char ** temp2;
bool Continue(false);
numnames = 0;
arraysize2 = firstguess;
names = newchar * [arraysize2];
do {
Continue = false;
numchars = 0;
arraysize = firstguess;
name = newchar[arraysize + 1];
cout << "Enter a name you want to sort: ";
while ((input = cin.get()) != '\n')
{
name[numchars++] = input;
if (numchars >= arraysize)
{
arraysize += firstguess;
temp = newchar[arraysize + 1];
memcpy(temp, name, numchars);
delete[] name;
name = temp;
}
}
name[numchars] = '\0';
if (name[0] != '\0') // down here is where I need help
{
names[numnames++] = name;
if (numnames >= (arraysize2 - 1))
{
arraysize2 += firstguess;
temp2 = newchar * [arraysize2];
cout << names[numnames - 1] << endl; // to compare to what comes after copying the memory
memcpy(temp2, names, sizeof(temp2)); // where the program seems to not do what I want
cout << temp2 [numnames - 1] << endl; // to test that it worked, it crashes here
delete[] names;
names = temp2;
}
Continue = true;
}
elseif (numnames == 0)
names[0] = '\0';
} while (Continue);
}