Originally Posted by
KingFlippyNips
Correct me if I'm wrong, but are you basically saying that I need to reference the null pointer instead of dereferencing something that is already null?
What do you mean by "reference the null pointer"?
I am saying that you need to take into account the possibility that the pointer could be a null pointer.
Actually, let's back up a bit. You wrote:
Originally Posted by
KingFlippyNips
The problem is as followed: "Modify the program you wrote for exercise 1 so that instead of always prompting the user for a last name, it does so only if the caller passes in a NULL pointer for the last name."
I assumed that this was a typo error, i.e., that the idea is the opposite: if you pass a null pointer, then the function does not prompt for a last name because there is no space for the last name; if you pass a valid pointer to a string, then the function prompts for a last name because there is space for the last name.
However, I checked the book and you're right: it does say "3. Modify the program you wrote for exercise 1 so that instead of always prompting the user for a last name, it does so only if the caller passes in a NULL pointer for the last name."
This means that this is wrong:
Code:
if(pLname){
cout << "Please enter your last name: ";
getline(cin, *pLname);
}
This asks the user to enter the last name if pLname is not a null pointer. You want the user to enter the last name only if pLname is a null pointer. This means that you should write:
Code:
if (!pLname){
cout << "Please enter your last name: ";
getline(cin, *pLname);
}
But of course this is wrong too: since pLname is a null pointer, you cannot dereference it.
What this means is that you need a string object for the last name:
Code:
string Lname;
// ...
cout << "Please enter your last name: ";
getline(cin, Lname);
Then you need to modify the way you create fullName to account for Lname.
EDIT:
Originally Posted by
KingFlippyNips
I tried approaching the problem using references instead of pointers and it seems to work just fine.
That's because the modification that you're trying to incorporate makes no sense when you're using references: there is no such thing as a null reference, so you cannot modify the program in that way. Therefore, you get what you did before you started trying to pass a null pointer as the second argument: it always works. Try passing a null pointer to your pointer version of the function before you modified it and it will likewise fail.