Reading from a Stream
Having asked the user for input, we next want to read that input. We start by defining two variables named v1
and v2
to hold the input:
int v1 = 0, v2 = 0;
We define these variables as type int
, which is a built-in type representing integers. We also initialize them to 0
. When we initialize a variable, we give it the indicated value at the same time as the variable is created.
The next statement
std::cin >> v1 >> v2;
reads the input. The input operator (the »
operator) behaves analogously to the output operator. It takes an istream
as its left-hand operand and an object as its right-hand operand. It reads data from the given istream
and stores what was read in the given object. Like the output operator, the input operator ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.