how to with function?

Jun 10, 2009 at 5:34am
hello, I can't figure it out, how is the right way?
char instead of string? pointers?, class?

Thanks

#include <iostream>
using namespace std;

string userInput;
void UserInput();

string UserInput()
{
cout << "Write string to print" << endl;
cin >> userInput;
return userInput;
}

int main() {

UserInput();
cout << userInput << endl; // prints user input
return 0;
}
Last edited on Jun 10, 2009 at 5:35am
Jun 10, 2009 at 6:07am
char instead of string? pointers?, class?


Just naming off random programming terms eh? None of those are your problem here.

Firstly, every time you use strings, you should #include <string> .

Second, your function declaration doesn't match its definition. You declare it as void and then in the actual definition you say it returns string.

Third, you don't really need that to be a separate function at all, this can all go in the main, along with your string variable.

Fourth, use getline() instead of cin. http://cplusplus.com/reference/string/getline/

This will fix all your problems
Last edited on Jun 10, 2009 at 6:43am
Topic archived. No new replies allowed.