Strings in function

For part of my project I have to prompt the user to enter their name, number, and email address using a function. I wrote the code for the function but when I try to call it in main it doesn't work. I found that if I call the variables in main AND in the function it works, but I feel like that's not the right way to do it. And I can't make the variables global either.

1
2
3
4
5
6
7
8
9
10
11
 void getInfo(string &, string &, string &)
{
    string name, number, email;
    cout << "Please enter your full name: ";
    getline(cin, name);
    cout << "Please enter you phone number: (ex. 555.555.5555) ";
    getline(cin, number);
    cout << "Please enter your email address: ";
    getline(cin, email);
    cout << endl;
}
Last edited on
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
#include <iostream>
#include <string>
#include <tuple>

std::string get_string( const std::string& prompt )
{
    std::string str ;
    std::cout << prompt << ": ";
    std::getline( std::cin, str ) ;
    return str ;
}

// tuple: https://en.cppreference.com/w/cpp/utility/tuple
std::tuple< std::string, std::string, std::string > get_info()
{
    // list initialisation: https://en.cppreference.com/w/cpp/language/list_initialization
    // note: in the braced-init-list, each initializer expression is sequenced before
    //       the initializer expression that follows it
    return { get_string( "Please enter your full name" ),
             get_string( "Please enter you phone number (ex. 555.555.5555)" ),
             get_string( "Please enter your email address" ) };
}

int main()
{
    // structured binding (C++17)
    // https://en.cppreference.com/w/cpp/language/structured_binding
    const auto [ name, phone, email ] = get_info() ;

    std::cout << "name: " << name << "\nphone: " << phone << "\nemail: " << email << '\n' ;
}
@JLBorges,

I was recently looking at structured binding, not very successfully, and you provide a very understandable example for using it. Thank you. :)
If you want a structure why not using one? See:
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
struct info_type
{
    string m_Name;
    string m_Number;
    string m_Email;
};

 info_type getInfo()
{
  info_type info;
    string name, number, email;
    cout << "Please enter your full name: ";
    getline(cin, info.m_Name);
    cout << "Please enter you phone number: (ex. 555.555.5555) ";
    getline(cin, info.m_Number);
    cout << "Please enter your email address: ";
    getline(cin, info.m_Email);
    cout << endl;
}

int main()
{
    const info_type info = get_info() ;

    std::cout << "name: " << info.m_Name << "\nphone: " << info.m_Number << "\nemail: " << info.m_Email << '\n' ;
}
Ill admit to having too many choices here. May be the nature of the simple task at hand, but choosing vector, std::array, tuple, struct, etc here I can't see much reason to pick one over another. Some of these lose the name of the individual pieces of data but I can't say that is a serious drawback. Just pick one for now, OP. Later, with more requirements, the choices may sort out cleaner.
Last edited on
Perhaps all he needed to do is properly name those parameters and remove the variable definitions inside the function?

1
2
3
4
 void getInfo(string &name, string &number, string &email)
{
    // string name, number, email; // Remove this line.
    cout << "Please enter your full name: ";
As much as I do like the std::tuple method, based on the OP's code snippet this also works:

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
#include <iostream>
#include <string>

void getInfo(std::string& name, std::string& number, std::string& email)
{
   std::cout << "Please enter your full name: ";
   std::getline(std::cin, name);
   std::cout << '\n';

   std::cout << "Please enter you phone number (ex. 555.123.4567): ";
   std::getline(std::cin, number);
   std::cout << '\n';

   std::cout << "Please enter your email address: ";
   std::getline(std::cin, email);
}

int main()
{
   std::string name  { };
   std::string phone { };
   std::string email { };

   getInfo(name, phone, email);

   std::cout << "\nname:\t" << name << "\nphone:\t" << phone << "\nemail:\t" << email << '\n';
}

Please enter your full name: Mork From Ork

Please enter you phone number (ex. 555.123.4567): 123.456.7890

Please enter your email address: b_will@reaper.org

name:   Mork From Ork
phone:  123.456.7890
email:  b_will@reaper.org
Topic archived. No new replies allowed.