So I'm trying to get familiar with c++. And here is the task that is supposed to exercise usage of pointers. Here is how it goes:
Write a function that prompts the user to enter his or her first name and last name, as two separate values. This function should return both values to the caller via additional pointer. It should prompt for the last name only if the caller passes in a NULL pointer for the last name.
I've tried a few versions. The one I'm stuck with now is:
#include <iostream>
#include <string>
using namespace std;
void getFullName(string *p_first, string *p_last) {
cout << "First name:";
getline(cin, *p_first);
if (!p_last) {
cout << "Last name:";
getline(cin, *p_last);
}
}
int main() {
string first;
string *p_first = &first;
string *p_last = NULL;
getFullName(p_first, p_last);
cout << *p_first << endl << *p_last << endl;
return 0;
}
Well, it crashes. And I've tried to pass a reference to the 'last' and then pointing to it. But after exiting the function the pointer is NULL again.