String Pointer Question

Hi. I don't have much experience with this, and I am having trouble getting this function to work. It's purpose is to print a question and ask for a response.

e.g. for request("what is your name?",string name)
i'd like it to store the user's response in string name.

1
2
3
4
void request(string question, string* input){
    cout<<question;
    getline(cin,input,'\n');
}


The compiler is telling me: "cannot convert 'std::string' to 'std::string*' for argument '1' to 'void request(std::string*, std::string)'|" Please help me fix it.
Pass it in using the unary & operator to get its address.
request("Hi, what's your name?",&s);
I think lovefromussr made a mistake in his question.
The code he posted says ( note the arrangement of the parameters ):
1
2
3
4
void request(string question, string* input){
    cout<<question;
    getline(cin,input,'\n');
}


buit the compiiler says that the parameters are the other way around:
The compiler is telling me: "cannot convert 'std::string' to 'std::string*' for argument '1' to 'void request(std::string*, std::string)
Yes, guestgulkan, you're right. I posted the wrong error message. The error should be: "cannot convert 'std::string' to 'std::string*' for argument '2' to 'void request(std::string, std::string*)'|"

rocketboy9000, sorry, I am afraid I don't fully understand. Is the function itself ok?

For example, I'd like the following program to ask for the users name, then print it:

1
2
3
4
5
6
7
8
9
string name;

void request(string question, string* input){
    cout<<question;
    getline(cin,input,'\n');
}

request("what is your name?", name);
cout<< name;


Should I use the & operator somewhere to fix this?
string name;

void request(string question, string* input){
cout<<question;
getline(cin,input,'\n');
}

request("what is your name?", name);
cout<< name;


Make your argument a reference like below then no need & when calling it.

1
2
3
4
5
6
7
8
9
string name;

void request(string question, string& input){
    cout<<question;
    getline(cin,input,'\n');
}

request("what is your name?", name);
cout<< name;
wow. simple solution. thank you, sohguanh.
Of cuz if you want to use & when calling you do something like below.

1
2
3
4
5
6
7
8
9
string name;

void request(string question, string* input){
    cout<<question;
    if (input) getline(cin,input,'\n');
}

request("what is your name?", &name);
cout<< name;
Topic archived. No new replies allowed.