I'm trying to calculate the length of three individual arrays and plug that length into a function to dynamically allocate memory to a new array and delete it. I keep overwriting my HEAP and I'm not sure where the problem is
void punchmeinthehead()
{
const int maxin = 16;
char lastName[maxin] ;
char firstName[maxin] ;
char midName[maxin] ;
size_t length = strlen(lastName) + strlen(firstName) + strlen(midName) + 1;
char *fullName = new char[length];
strcpy (fullName, lastName);
strcat (fullName, " ");
strcat (fullName, firstName);
strcat (fullName, " ");
strcat (fullName, midName);
cout << fullName << endl;
delete [] fullName;
}
Main looks like this
void main()
{
const int maxin = 16;
char lastName[maxin];
char firstName[maxin];
char midName[maxin];
getlast(lastName, maxin);
getfirst(firstName, maxin);
getmid(midName, maxin);
punchmeinthehead();
}
The calling functions look like this
void getlast( char lastName[] , int maxin)
{
cout << " Please enter your last name up to 15 characters " << endl;
cin.getline(lastName, maxin, '\n' );
cin.ignore (cin.rdbuf()->in_avail(), '\n');
}
//Input first name
void getfirst( char firstName[] , int maxin)
{
cout << " Please enter your first name up to 15 characters " << endl;
cin.getline(firstName, maxin, '\n' );
cin.ignore (cin.rdbuf()->in_avail(), '\n');
}
//Input middle name
void getmid( char midName[] , int maxin)
{
cout << " Please enter your middle name up to 15 characters " << endl;
cin.getline(midName, maxin, '\n' );
cin.ignore (cin.rdbuf()->in_avail(), '\n');
}