Input/Output

When a user runs a program, they are presented with a screen called the terminal. Through this screen the program may display information or accept information from the user. We call displaying information output and accepting information from the user input

Streams

Input and output are performed through streams. A stream is a datatype which can take in data or give out data (or both). The two main streams used in C++ are cin for input and cout for output.

In order to interact with streams, we have two operators << and >>, the usage is as follows:

stream << variable; // this feeds the value of the variable into the stream
stream >> variable; // this takes data from the stream and stores it in variable

In the first case, variable is unaffected, but in the second case the variable will have a new value.

Note

“Stream” in this context is a type, whereas cin and cout are variables of that type.

To be able to work with streams, and use cin and cout, we must add #include <iostream> to the beggining of our program.

Stream cout

Suppose you wrote the following program:

int x = 5;
// add 1 if x is divisible by 3 and add 2 otherwise
int y = x + (x % 3 == 0 ? 1 : 2);

The variable y is stored in memory. The user has no way of knowing its value, if you want to share this variable with the user you must output it to the terminal.

cout << y;

This will display the value of y on the terminal. If we wish to print regular text we use "...".

cout << "The value of y is";
cout << y;

The result is…

The value of y is7

This doesn’t look quite right. The reason is because C++ does not understand what looks right and what doesn’t. C++ performs the operations exactly as we describe them. Since we never specified that there needs to be a space after “is”, the program didn’t add one. Instead, we should write:

cout << "The value of y is ";
cout << y;

We can print multiple expressions in the same line:

cout << "The value of y is " << y;

We can also print a new-line character '\n' to jump to a new line:

cout << "Hello" << '\n' << "World";

We get:

Hello
World

Stream cin

We know how to write a program which defines, interacts with and prints data. The problem is that the program does the exact same thing each time it’s run. We might want to write a program which allows the user to choose the values on each run. For that we will use cin.

int x;
cin >> x;
cout << "You entered: " << x;

Here the program will wait on the instruction cin >> x until the user writes a value in the terminal and hits enter. After thats done, the value will be stored in x.

Example input:

5

Expected output:

You entered: 5
Note

Notice that the operators for cin and cout are different: >> for cin, and << for cout.

Function getline

When we use cin the stream reads until it finds a white space or we reach the end of the input file. Sometimes, we might want to read a full line even if it contains spaces. For that we use getline.

string s;
getline(cin, s);

cout << "The line is: " << s;

To use getline you must add two arguments, the stream that it should read from, and the variable it should store the result in.

Caution

It is not recommended to use cin and getline in the same program.

Example problem

Example problem

Write a program which reads 2 integers and prints first the smaller one and then the larger one, on separate lines.

Solution to the example problem

The steps to solve this problem are as follows:

  • Declare two integer variables in order to store the input.
  • Use cin to read the input.
  • Declare two additional variables to store the answer.
  • Use ternary operator to determine the smaller and larger values.
  • Use cout to print the answer.
int a, b;
cin >> a >> b; // we can read multiple variables in the same line

int small = (a < b ? a : b);
int large = (a > b ? a : b);

cout << small << '\n' << large;

Fast I/O

When you use cout the data you print does not necessarily get printed right away. Sometimes it is delayed (stored in a buffer) to speed up the execution. By default, whenever you use cin the buffer gets flushed, which can sometimes be slow.

Additionally, if the buffer already has lots of data it might randomly decide to flush the output during execution. We can untie cin from cout and tell cout to only flush once at the end of execution by adding the two following lines:

cin.tie(0); // tie the cin stream to stream (0) i.e no stream
cin.sync_with_stdio(0);
Important

In the competitions, programs are tested automatically. The input is not entered by a user, instead it is redirected from a file. Such inputs can be very large. Whenever a problem has a lot of input (hundreds of thousands of numbers), you want to have the previous lines at the beginning of your code, otherwise it might run to slow.