Thread: Using Pointers

  1. #1
    SnoopFrogg KingFlippyNips's Avatar
    Join Date
    Sep 2016
    Posts
    33

    Using Pointers

    Recently, I have been learning C++ using the book "Jumping Into C++" and I had a question about one of the exercises. The exercise is as follows:

    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. (Hint: the function signature will look be similar to the swap function from earlier!)

    Here is what my solution looks like:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void nameSwap(string *pFname, string *pLname);
    
    int main(){
    
        string firstName;
        string lastName;
    
        cout << "Please enter your first name: ";
        getline(cin, firstName);
        cout << "Please enter your last name: ";
        getline(cin, lastName);
        nameSwap(&firstName, &lastName);
        cout << "Nice to meet you " << lastName << " " << firstName << "!" << "\n";
    
        return 0;
    }
    
    void nameSwap(string *pFname, string *pLname){
    
        string temp = *pFname;
        *pFname = *pLname;
        *pLname = temp;
    }
    When I run the code it works but I'm not sure if I'm doing what the problem is asking. It doesn't ask to swap the names, so that's why I wrote the nameSwap function like that. Any help is greatly appreciated. Thanks!
    Last edited by KingFlippyNips; 12-14-2018 at 05:01 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    It's not asking you to swap names; it's saying that an earlier function for swapping can give you an idea of what this function might look like. The function that you're supposed to write is about reading in the names.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    SnoopFrogg KingFlippyNips's Avatar
    Join Date
    Sep 2016
    Posts
    33
    Quote Originally Posted by laserlight View Post
    It's not asking you to swap names; it's saying that an earlier function for swapping can give you an idea of what this function might look like. The function that you're supposed to write is about reading in the names.
    Ah, thanks for clearing that up for me. Much appreciated!

  4. #4
    SnoopFrogg KingFlippyNips's Avatar
    Join Date
    Sep 2016
    Posts
    33
    I've been a bit busy but I solved the problem thanks to your clarification. Thanks again!

    Here's the solution I came up with:


    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string name(string *pFname, string *pLname);
    
    int main(){
        
        string firstName;
        string lastName;
    
        cout << "Nice to meet you " << name(&firstName, &lastName);
    
        return 0;
    }
    
    string name(string *pFname, string *pLname){
    
        cout << "Please enter your first name: ";
        getline(cin, *pFname);
        cout << "Please enter your last name: ";
        getline(cin, *pLname);
    
        string fullName = (*pFname) + " " + (*pLname);
        return fullName;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2015, 01:15 PM
  2. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  3. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM

Tags for this Thread