I am teaching myself C++ and am working through exercises in a tutorial book. In the current problem, I am supposed to pass in a null pointer to a method for the last name parameter that triggers a prompt for assigning the last name.
My problem is that this either:
- Throws an exception.
- Does not carry back outside of the method if I assign in ways to avoid the exception.
- Seems to require a double pointer or reference, based on my current search findings. However, I need to use a pointer parameter for the method (I use a reference parameter as another exercise), and double pointers are not introduced yet in the text (so I feel like this is cheating/missing the point of the lesson).
How do I assign to a string null pointer such that it is preserved outside of the method without using references or double pointers?
Current code:
#include <iostream>
#include <string>
using namespace std;
void getNameByPointer(string* firstName, string* lastName)
{
cout << "Please enter your first name: ";
cin >> *firstName;
// Check for null.
// Only request last name IF null, as per the book exercise statement.
if (!lastName)
{
string tempLastName;
cout << "Please enter your last name: ";
cin >> tempLastName;
// Note: All assignment methods below fail in some way if lastName is null.
// I cannot find a way to assign to a null pointer that carries out
// of the method without using a double pointer or pointer
// reference, which are not yet introduced in the book.
// (1) Throws exception 'Read Access violations': std::_String_alloc<std::_String_base_types<char,std::allocator<char> > >::_Myres(...) returned 0x18.
// *lastName = tempLastName;
// (2) This assignment is limited to the method scope.
// lastName = &tempLastName;
// (3) This assignment is limited to the method scope.
// lastName = new string(tempLastName);
}
}
int main()
{
string firstName;
string lastName;
getNameByPointer(&firstName, &lastName);
cout << "Your name is " << firstName << " " << lastName << '\n';
cout << "Calling method with NULL...\n";
string firstName1;
// Null pointer
string *lastName1 = nullptr;
getNameByPointer(&firstName1, lastName1);
if(lastName1)
{
cout << "Your name is " << firstName1 << " " << *lastName1 << '\n';
}
else
{
cout << "Your name is " << firstName1 << '\n';
}
}
Edit: To better clarify if I have misread the problem, or better communicate my restraints, the problem is stated below:
Exercise 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.
(Exercise 1 has already been solved but is the basis for Exercise 3.)
Exercise 1
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 (or reference) parameters that are passed to the function. Try doing this first with pointers and then with references.
(For Exercise 3, a similar solution was done for the reference function by just checking if the variable is empty as NULL cannot be checked for in the reference parameter.)
empty().void getNameByPointer(string* firstName, string*& lastName)?