Writing to the console

Console input/output with the C++ standard library

To read/write text and numbers (Texts are actually sequences of number representing characters) from/to the console, the C++ standard library provides the objects cin and cout. What are objects? They are variables of special types called classes. That is a relatively advanced subject that we will learn about later, so for now, we will only see how to use them.

Note that cin and cout are part of the namespace std. So, if the line using namespace std; is absent of our project, we must write std::cin and std::cout.

The object std::cin is used to read from the console and the object std::cout is used to write to it.

The objects std::cin and std::cout are defined in the header file <iostream>, so we must include it in order to be able to use them. It is done by adding the following line at the beginning of the code file:

#include <iostream>

Using the object std::cout

In order to output something to the console, we must use the output stream operator << with the object std::cout as left operand and the thing to output to the console as right operand.

Writing a number to the console

To output a number to the console, we simply have give a number as right operand to the stream operator <<.

Example:

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

Writing the value of a variable to the console

To output the value of a variable to the console, we simply have to give that variable as right operand to the stream operator <<.

Example:

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

Writing text to the console

We write text to the console by writing the text to output inside quotes as the right operand of the output stream operator <<.

Example:

1
2
3
4
5
6
7
8
#include <iostream> int main() { std::cout << "I am a text."; return 0; }

New line

To output a new line to the console (Set the cursor to the beginning of the line below), we can simply give the object std::endl as right operand to the output stream operator <<.

1
2
3
4
5
6
7
8
9
10
#include <iostream> int main() { std::cout << "I am above."; std::cout << std::endl; std::cout << "I am below."; return 0; }

It is also possible to insert the set of character \n where we want the new line to appear inside the text to output. When the \n is reached, the cursor of the console is set to the beginning of the line below.

The result of the following example is the same as the one above:

1
2
3
4
5
6
7
8
#include <iostream> int main() { std::cout << "I am above\nI am a line below.\n"; return 0; }

Writing the result of an operation

The right operand of the << operator can be an operation.

The following code will output, in the console, the result of the multiplication of the variable var by 6.

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

Outputting multiple things a once

The following:

1
2
3
4
5
6
7
8
9
#include <iostream> int main() { int var = 7; std::cout << "Value of var: " << var << std::endl; return 0; }

Produces the same result as:

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

What happens in the line std::cout << "Value of var: " << var << std::endl; is that the stream operation std::cout << "Value of var: " is executed and returns back the object std::cout. The next output stream operation (<< var) then receives the object std::cout returned, as left operand (std::cout << var). The operation is executed and the object std::cout is returned again. Finally, the last output stream operation receives the returned std::cout object as left operand (std::cout << std::endl).