I'm working with an exercise and it tells me i need to use a new command and strcpy, i don't really understand how strcpy works i've been reading up on it, but for some reason just can't figure out what exactly needs to be done. i'm gonna list out the code, this is a homework assignment for a class so i'm not looking for a complete solution to this, just a little guidance.
// exercise driver
#include "person.h"
#include <iostream>
usingnamespace::std;
void main()
{
person boss("Tom", 42);
// sending the return value to cout, test the 3 ways of getting the age
person * p;
p = new person("Susan", 31);
// sending the return value to cout, test the 3 ways of getting the age
// test your member functions
return;
}
strcpy takes 2 pointers. It copies the string data from one pointer into the other pointer.
For example:
1 2 3 4 5 6
char a[] = "This is a cstring buffer";
char b[100]; // this is another buffer
strcpy(b,a); // this copies the string contained in 'a' to 'b'
cout << b; // this prints "This is a cstring buffer"
Now since your person class only has a char pointer, you need to dynamically allocate the buffer to hold the person's name. The buffer must be large enough to hold the full name + 1 (for the null termination character).
Presumably, you learned out to get the length of strings and how to use new[] and delete[] already.