Thread: Jumping into C++ Ch 13 Problem 3 - Pointers

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    Cape Cod, MA
    Posts
    1

    Jumping into C++ Ch 13 Problem 3 - Pointers

    Hey all, I'm not sure what problem 3 of Jumping into C++ is asking me to do. For those of you who do not have the book, I'll reprint it here.

    "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."

    The first exercise it refers to has us use pointers (or references) to have a function that changes the value of two strings (calling by reference I believe?). My code for the fist problem is as follows:
    Code:
    #include <iostream>#include <string>
    
    
    using namespace std;
    
    
    void names(string *p_first, string *p_last)
    {
        cout << "What is your first name?:\n";
        cin >> *p_first;
        cout << "What is your last name?:\n";
        cin >> *p_last;
    }
    
    
    int main()
    {
        string firstName, lastName;
        names(&firstName, &lastName);
        cout << firstName << " " << lastName;
        return 0;
    }
    So yea, just don't know what the question is asking. Does it just want me to add an
    Code:
    if (p_last == NULL)
    {
    //asks for last name etc.
    }
    in the names() function? Seems a bit trivial but maybe I'm over-thinking this

    thanks

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Your solution to this seems correct.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    asking for a name only if the incoming pointer is NULL seems a bit nonsensical to me. if it's null, you can't dereference it. since you're passing with just a plain pointer to string, you can't return a string created by new through the parameter. the task seems pointless to me, and I don't understand what the author expects you to learn from it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I missed the conflict with the problem statement in my previous post. I think the problem statement has this backwards. Perhaps the idea is to only ask for a first or last name if the corresponding pointer to string is not NULL, so that there is a place to store the response.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I think this whole problem is backward - C style thinking way programming.

    Function should receive a references not pointers and be overloaded to get only one parameter if user does not needs last name instead of relaying on C-style NULL pointer checking
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping Into C++ - Chapter 8 Problem[3]
    By Mohamed Adel in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2013, 09:14 AM
  2. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM
  3. Jumping into C++ ebook problem
    By tonyk in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2012, 05:19 AM
  4. Jumping into C++ PracticeH Problem
    By Sid_TheBeginner in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2012, 10:00 AM
  5. Jumping into C++ Chapter 5 problem 6 - Critique please
    By Kranky in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2012, 05:44 PM

Tags for this Thread