How to interpret the question?

How do I interpret the following practice problem:

(I have done the problem and finished it but I can't tell if I have finished it)

[from a 'popular' book]
"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"

Note ex 1 is to use pointers first and then references second to call a function where the user is prompted for their first and last names. The point is that a function returns only one variable, so you need to use a pointer/reference as an input argument into which you can write the second variable (string in this case). I did this ok and it is part of the code below.

This is from a chapter where the use of * and & are introduced.

My question:
This fails. I think it is supposed to, and I think that is the learning point, do you agree? ... I ask because I feel like there may be an implication that this is achievable, but I can't see how.

When sending a NULL pointer to a function like this you (probably going to) write to it's dereference location. When you do this it is not pointing anywhere, so it just hangs. Lesson: don't write to a NULL pointer. (yes I am using nulptr for NULL in the code).

Really this question is about the problem above not the code below. How would you interpret the question?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  #include <iostream>
#include <string>

using namespace std;

void get_name_ptrs(string * first_name , string * last_name);
void get_name_addrs(string & first_name , string & last_name);
void print_name_ptrs(string * first_name , string * last_name);
void print_name_addrs(string & first_name , string & last_name);

int main()
{
    // set up the variables
    string  name_part_one,  name_part_two;
    // get the users name
    cout << "hello" << endl;
    //make a blank pointer
    string * _p_name_part_one = nullptr, * _p_name_part_two = nullptr;

    // get the users name  by ptrs
    cout << "going to call get_users_name  by ptrs and passing address" << endl;
    cout << "but first lets see whats in it: " << name_part_one << " : " << name_part_two << endl;
    cout << "calling get_users name  by ptrs and passing address" << endl;
    get_name_ptrs(&name_part_one, &name_part_two);
    // then print it out   by ptrs
    cout << "then print_users name  by ptrs and passing address" << endl;
    print_name_ptrs(&name_part_one, &name_part_two);

    // then print it out by ptrs and passing ptr with null
    cout << "calling get_users name  by ptrs and passing ptr with null" << endl;
    cout << "since it is  null it will get a name" << endl;
    get_name_ptrs(_p_name_part_one, _p_name_part_two);
    // then print it out by ptrs and passing ptr with null
    cout << "then print_users name  by ptrs and passing ptr with null" << endl;
    print_name_ptrs(_p_name_part_one, _p_name_part_two);

    //assifn an address tp the pointers
    _p_name_part_one = &name_part_one;
    _p_name_part_two =  & name_part_two;
    // then print it out by ptrs and passing ptr with null
    cout << "calling get_users name  by ptrs and passing ptr with addreess" << endl;
    cout << "since it is not null it will probably do nothing" << endl;
    get_name_ptrs(_p_name_part_one, _p_name_part_two);
    // then print it out by ptrs and passing ptr with null
    cout << "then print_users name  by ptrs and passing ptr with adress" << endl;
    cout << "this function does not care about null so it will print the old name" << endl;
    print_name_ptrs(_p_name_part_one, _p_name_part_two);

    // then get the users name  by addr
    cout << "and finally: " << endl;
    cout << "calling get_users name refs and passing refs with normal varisbles names" << endl;
    cout << "this function internally uses refs" << endl;
    get_name_addrs(name_part_one,  name_part_two);
    // then print it out  by addr
    cout << "calling print_users name refs and passing refs with normal variables names" << endl;
    cout << "this function internally uses refs" << endl;
    print_name_addrs(name_part_one,  name_part_two);

    //and finish
    cout << "All done, goodbye." << endl;

    return 0;
}

void get_name_addrs(string & first_name , string & last_name)
{
    cout << "Please enter your first name by addr: " << endl;
    cin >> first_name;
    cout << "Please enter your Surname name by addr: " << endl;
    cin >> last_name;

    return;
}

void print_name_addrs(string & first_name , string & last_name)
{
    cout << "printing first name by addr: " << first_name << endl;
    cout << "printing Last name by addr: " << last_name << endl;
    return;
}

void get_name_ptrs(string * first_name , string * last_name)
{
    if ( last_name == nullptr)
    {
        cout << "Please enter your first name by ptrs: " << endl;
        cin >> *first_name;
        cout << "Please enter your Surname name by ptrs: " << endl;
        cin >> *last_name;
    }

    return;
}

void print_name_ptrs(string * first_name , string * last_name)
{
    cout << "printing first name by ptrs: " << *first_name << endl;
    cout << "printing Last name by ptrs: " << *last_name << endl;
    return;
}


The preview option does not work on my setup so can't check layout.

It hangs at line 37/87, and the OS catches it and closes it.
Last edited on
I would suggest that you remove 18 completely and pass the pointer directly:

print_name_ptrs(&name_part_one, & name_part_two);

Your current problem is line 32/35 because both _p_name_part_one and _p_name_part_two are nullptr.
Topic archived. No new replies allowed.