I've heard that pointers can be a pain in the butt to implement and now I know.. Basically, I'm supposed to input a last, first and middle name. Then compute the size needed for a dynamic array to hold the full name. After that dynamically allocate the array, check DMA success and do all the copying. Once the copying and concatenating is done pass it to my function and display the results. To me passing to a function to display is silly for such a small program, but that's what my instructor wants.. lol.. I wrote the code to do all the above minus passing to a function and instead just using cout to display, but when I tried to implement it in a function I got thrown a curve ball. I keep running into an error..
ERROR: cannot convert parameter 1 from 'char *' to 'char'
Here's the source code.
HEADER CODE:
#ifndef Lab5_h
#define Lab5_h
void DisplayName (char pFullName);
#endif
CPP CODE:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <cstring>
using std::strcpy;
using std::strlen;
using std::strcat;
#include "Lab5.h"
int main ()
{
char FirstName[20];
char MiddleName[20];
char LastName[20];
char *pFullName;
int Choice = 0;
int Sum = 2;
while (Choice != 9)
{
cout << "Please input your last name.. (i.e. Doe) ";
cin >> LastName;
cout << "Now, input your first name.. (i.e. John) ";
cin >> FirstName;
cout << "Finally, input your middle name.. (i.e. Edward) ";
cin >> MiddleName;
Sum = strlen (LastName) + strlen (FirstName) + strlen (MiddleName);
cout << "Sum is " << Sum << endl;
pFullName = new char[Sum];
if (pFullName == 0)
{
cout << "Memory allocation failed -- exiting!\n";
exit (1);
}
strcpy (pFullName, FirstName);
strcat (pFullName, " ");
strcat (pFullName, MiddleName);
strcat (pFullName, " ");
strcat (pFullName, LastName);
DisplayName(pFullName);
delete [] pFullName;
cout << "Input 9 to QUIT..\n";
cout << "Or 1 to CONTINUE with a new name.. ";
cin >> Choice;
}
}
void DisplayName (char pFullName)
{
cout << pFullName << endl;
}