简体   繁体   中英

c++ return string or char from user input

im trying to return a string/char (tried with both) from a funtion:

 char* foo(void)

    {

    using namespace std;
   // char Name[100];
    std::string Name;
    std::cout << "Type your name : ";
    //std::getline(std::cin, Name);
    std::cin >> Name;

    // std::cout << "Hello " << Name << "\n";

   // std::string str(Name);
    return Name;
    }

which gives me this error:

/home/peter/netsend/main.cpp:22: error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'char*' in return return Name; ^

If i use char Name[100]; i get this warning when i compile my program:

/home/petter/netsend/main.cpp:13: warning: address of local variable 'Name' returned [-Wreturn-local-addr] char Name[100]; ^

when i run it i get:

ype your name : hh terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid Aborted (core dumped)

if i run this directly in int main it works:

using namespace std;
   // char Name[100];
    std::string Name;
    std::cout << "Type your name : ";
    //std::getline(std::cin, Name);
    std::cin >> Name;

    // std::cout << "Hello " << Name << "\n";

   // std::string str(Name);

then i could use Name in my other functions in int main. any ideas?

Thanks

Just return a string , like this:

auto foo()
    -> std::string
{
    using namespace std;
    string Name;
    cout << "Type your name : ";
    getline( cin, Name );
    return Name;
}

Or instead of the C++11 "trailing return type" syntax, use the old syntax

std::string foo()

… which means the same and is still much more common, but can't be used in every case.

I will divide your problem into cases which I will describe briefly.

  1. If you create a local array, namely char Name[100] and return it, you will end up with a dangling pointer . Name[100] is an automatic variable which means that once the function goes out of scope the pointer that is returned by the function will point to an invalid memory location.

  2. return Name when Name is an std::string . Well, here you have a type mismatch. Your function returns a char* and you return a std::string . Hence, your compiler issues an error.

To fix your issue declare Name as a std::string and declare the function to return a std::string like this: std::string foo(void) .

In the first case, you should probably return a string (ie, std::string foo(void) { ... } ) and then simply use string::c_str() to get the C-style string from it when/if needed.

In the second case, the compiler is exactly right. You cannot safely use a variable after its scope has ended, that will be undefined behaviour. The char array ceases to exist when the function exits and you're returning the pointer to its first character, a big no=-no.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM