Pass data to other functions?

hi guys,

i was wondering how i can pass data to a function parameter using cin>>
You mean get data with cin>> and passing that data to a function?
If you mean something like this:
1
2
3
4
5
6
7
8
9
10
void MyFunc(std::string Str) 
{
    std::cout << "You said: " << Str << std::endl;
}

int main(void)
{
    MyFunc(cin >>);  // No go
    return 0;
}

..you can't. std::cin takes a reference to an object where it stores the input and in that piece of code there is nothing to refer to.

You'd have to do:
1
2
3
4
5
6
7
8
9
10
11
12
void MyFunc(std::string const &Str) 
{
    std::cout << "You said: " << Str << std::endl;
}

int main(void)
{
    std::string Str;
    std::cin >> Str;
    MyFunc(Str);
    return 0;
}


Or:
1
2
3
4
5
6
7
8
9
10
11
12
void MyFunc(std::istream &Stream) 
{
    std::string Str;
    Stream >> Str;
    std::cout << "You said: " << Str << std::endl;
}

int main(void)
{
    MyFunc(std::cin);
    return 0;
}
Topic archived. No new replies allowed.