0

I am very confused as to what kind of variables I would put into my function here: names. I am doing a practice problem in a C++ book, because I am learning C++ and am on References and pointers right now, and cannot find a solution.

Just for background information, the problem asks:

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.

#include <iostream>
#include <string>
#include <istream>

using namespace std;

struct someStruct{
    string firstname;
    string lastname;
};

void names(someStruct &firstname, someStruct &lastname) {
    cout << "First Name: " << "\n";
    cin >> firstname.firstname;
    cout << "Last Name: " << "\n";
    cin >> lastname.lastname;
    // I was just curious is adding firstname to firstname would work... and     it did
    cout << lastname.lastname << ", " << firstname.firstname;
    cin.get();
}

int main() {
    names();
    // I don't know what to put here, above, as parameters
    cin.get();
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Max Khan
  • 21
  • 10

1 Answers1

1

Your code makes no sense, why are you passing someStruct twice?

For the reference part, you should have something like:

void names(someStruct &s) { // <<<< Pass struct once as a reference
    cout << "First Name: " << "\n";
    cin >> s.firstname;
    cout << "Last Name: " << "\n";
    cin >> s.lastname;
}

and in main():

int main() {
    someStruct x; // <<<< Create an instance of someStruct
    names(x); // <<<< Pass it as parameter

    cout << "Input was: firstName = " << x.firstname 
         << ", lastName = " << x.lastname 
         << endl;
    cin.get();
}

For the pointer part, you should have something like:

void names(someStruct *s) { // <<<< Pass struct once as a reference
    cout << "First Name: " << "\n";
    cin >> s->firstname;
         // ^^ Note the difference in dereferencing
    cout << "Last Name: " << "\n";
    cin >> s->lastname;
         // ^^ Note the difference in dereferencing
}

and in main():

int main() {
    someStruct x; // <<<< Create an instance of someStruct
    names(&x); // <<<< Pass the address of x as parameter
       // ^ Note the addess-of operator here

    cout << "Input was: firstName = " << x.firstname 
         << ", lastName = " << x.lastname 
         << endl;
    cin.get();
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Ohh, I did not think about it that way. Thank you. – Max Khan Jun 22 '15 at 21:55
  • Also, I thought that whenever you set parameters that involved references, in a function, that they had to point to an initialized value. Or, does setting the parameter, initialize the value? I am confused on how this function works, when it is referencing to a value that isn't initialized anywhere else. An explanation would help. – Max Khan Jun 22 '15 at 22:02
  • @MaxKhan Values are initialized by the compiler generated default constructor of `someStruct` using the default constructors for the contained member variables (`std::string::string()` for your case => empty `string`), unless you define a default constructor yourself, and initialize them using the [_member initializer list_](http://stackoverflow.com/questions/7665021/c-member-initialization-list). – πάντα ῥεῖ Jun 22 '15 at 22:13
  • Ok, I get it now. Thanks, – Max Khan Jun 22 '15 at 22:17