std::cin >> x
returns a reference to cin
, which isn't implicitly convertible to int
.
You can use the ,
operator like so:
(std::cin >> x, x)
to first run std::cin >> x
and then evaluate that expression to x
.
#include <iostream>
int doubleNumber(int a)
{
return 2 * a;
}
int main()
{
int x;
std::cout << doubleNumber( (std::cin >> x, x) );
return 0;
}
Splitting it will probably into two lines will probably make it more readable, though.
In any case std::cin >> x
can be used as an expression.
E.g., it's common to have streams implicitly converted to booleans to check whether they're in a succeeding (good) state. (E.g., if(std::cin >> x){ //...
)