direct function input

Is it possible to input, from keyboard, one or more parameters to a function without first saving them to a variable?

For example raplacing
1
2
3
            cout << "Ange filnamn\n";
            getline(cin, tempString);
            writeFriendsOnFile(kompisar, antalKompisar, tempString);

with something like:
1
2
            cout << "Ange filnamn\n";
            writeFriendsOnFile(kompisar, antalKompisar, cin.get());
Your only solution is to set one of the arguments of the function to an istream reference so that you can pass it cin. Then it can handle the input itself.
You can make a helper function
1
2
3
4
5
6
7
8
9
std::string my_getline(std::istream& in) {
    std::string line;
    if (!std::getline(in, line)) {
        //input failed, throw some exception or return an empty string
    }
    return line;
}
//you can call functions without an intermediate variable
foo(my_getline(std::cin));
Ok, thanks.
Helper function seems straight forward. I'll look into the istream reference option as well.

Edit. Oh, just realised that the helper function is also the istream reference option...
Last edited on
Topic archived. No new replies allowed.