Hi Everybody,
Thanks so much for your help so far. I will respond to one of the replies and then show the code where I see my error.
Jiggunjer, I tried running your code and it works, but I'm not sure why.
In the book, he hasn't done stuff like this...
Code:
void getnames(char*& p)
{
cin>>nameone; //includes '\0' at the end
p=nameone;//assign our pre-sized array to ptr1
}
Why do you do char*& p? I'm not sure what the "&" is there for.
Here is my attempt at using a char array, but I'm getting an error. I will post the code along with the error.
Code:
#include <iostream>
using namespace std;
void return_names(char *p_first, char *p_last)
{
char first[20], last[20];
cout << "Please enter the first name: ";
cin >> first;
cout << "Please enter the last name: ";
cin >> last;
p_first = first;
p_last = last;
}
int main()
{
char first_name[20], last_name[20];
return_names(&first_name, &last_name);
cout << "The full name is: " << first_name << " " << last_name;
return 0;
}
Here is the error:
cannot convert 'char (*)[20]' to 'char*' for argument '1' to 'void return_names(char*, char*)'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
I don't really understand this error. Can somebody give me a clue how my code could be modified so that it works?
Also, in my original code, for the return_names function, I wrote this:
However, it seems once I modified it to use the char array I got an error when I tried dereferencing p_first and assigning it to first, so I got rid of the '*' and the error went away. Why is that?
Thanks,
Ben