Reading from the console

Using the object std::cin

To read from the console using the object std::cin, we must use the input stream operator >> with std::cin as left operand.

Once an input stream operation, with std::cin as left operand, is reached, the program will wait for the user to write something in the console and press the enter key. When this is done, the input is saved in the right operand of the input stream operator and the program continues its execution.

Note that the input stream operations on std::cin read one word at a time. Therefore, if the user enters two 'words', for example 100 and 82, the first input stream operation will only read the number 100 and the 'word' 82 will stay there for the next input stream operation to retrieve.

Reading a number from the console

To read a number from the console into a variable, we simply give the variable as right operand to the input stream operator >>, with std::cin as left operand.

Example:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream> int main() { int num; std::cin >> num; std::cout << num << std::endl; return 0; }

Reading text from the console

Reading text from the console works the same way as reading numbers, except that the variable as right operand of the input stream operator >> needs to be of a type that can hold text. Since we have not seen how to handle text yet, we will learn how to input text from the console later.

What if the user enters unexpected data?

If the user enters text into the console while a number is expected, the program will fail. We will see how to manage that later, so for now we assume the user enters valid data.

Inputting many things in one instruction

Similarly to output stream operations, input stream operations return the object std::cin back, so the following code:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream> int main() { int var, var2; std::cin >> var >> var2; std::cout << var << " " << var2 << std::endl; return 0; }

Has the same effect as:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream> int main() { int var, var2; std::cin >> var; std::cin >> var2; std::cout << var << " " << var2 << std::endl; return 0; }

Input vs output

The input stream operator >> (used with std::cin) points to the opposite direction of the output stream operator << (used with std::cout). A way to help us remember which operator to use with which object is to tell ourselves that since std::cin is used to read something from the console to a variable, the operator >> points toward the variable, while with std::cout, the content of the variable goes to the console, so the operator << points toward std::cout.

We can also easily remember the name of the objects std::cin and std::cout by telling ourselves that cin stands for Console INput and cout for Console OUTput. This is not exactly what they stand for, by I believe it is easier to remember them like that.

Practicing what we learned so far

Before going on with learning C/C++, I recommend you to practice working with variables. Now that we know how to output the value of a variable to the console, we can execute operations on variables and see if the result is what we expected.

Here is the code of a small program we should be able to understand now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> int main() { int num; int num2; std::cout << "Enter an integer number: " << std::endl; std::cin >> num; std::cout << "Enter an other integer number: " << std::endl; std::cin >> num2; std::cout << "The summation of the two numbers entered is " << num + num2 << " !" << std::endl; return 0; }

Remember that the execution of the program happens inside the brackets {} after line int main(). It is executed from top to bottom.