Reputation: 21
like
if a
, b
are variables
then
cin>>a>>b;
like this how many variables can I take with 1 cin
.
Upvotes: 0
Views: 430
Reputation: 764
Actually, it is unlimited. But usually people use loops to input more data from cin, it is more controllable.
Upvotes: 0
Reputation: 122458
Fist some nitpicking on wording: There is only one std::cin
. It is an object of type std::istream
. It has an operator>>
that lets you read one thing at a time. As the operator returns a reference to std::cin
you can chain as many calls as you like.
Consider that those two are doing the same thing:
std::cin >> a;
std::cin.operator>>(a);
Chaining is achieved by
std::cin >> a >> b;
std::cin.operator>>(a).operator>>(b);
Because each call to operator>>
returns a reference to the stream, there is no limit on how many variables you can read in one statement:
int a,b,c,d,e,f;
std::cin >> a >> b >> c >> d >> e >> f;
Though already with 2 variables you should consider if maybe they belong into the same data structure
struct a_and_b {
int a;
int b;
};
Then you can provide an overload for the operator>>
std::istream& operator>>(std::istream& in,a_and_b& x) {
in >> x.a;
in >> x.b;
return in;
};
And then use the much more readable:
a_and_b x;
std::cin >> x;
Upvotes: 7