"Local variables are destroyed and their memory reclaimed at the end of the enclosing block." - cprogramming.com
That means that you are making reference to a memory location that does not exist anymore. One way to get around this is to use a global variable as in the next example.
#include <iostream>
#include <string>
using namespace std;
string name;
string *acceptName(){
cout << "Name:";
cin>> name;
return &name;
};
int main() {
string *name;
name = acceptName();
cout<< "Your name is "<< *name<< endl;
return 0;
}
This works fine - but as you know Global varaibles is not such a good idea.I will look for some more ways to work around this.