Input/Output

Input/Output is what I/O stands for, it simply refers to data transmission involving a computer. Related to this is the concept of a stream which is an interface for transmitting data in an ordered sequence.

Many operating systems provide standard streams to every process (an executing program): standard input, standard output, and standard error.

C++ provides access to standard input through cin, standard output through cout, and standard error through cerr.

Unless redirection is used, a program executed through an interactive command shell will have it's standard streams associated with the text terminal; that is: keyboard input will be sent to standard input, and standard output and standard error will be sent to the terminal display.

C++ mainly distinguishes between 3 types of streams. The type for cin is istream (which stands for input stream), and the type for cout and cerr is ostream (which stands for output stream). The 3rd type of stream C++ supports is iostream (input/output stream).

An input stream only allows one to receive data from it while an output stream only allows one to send data to it, an input/output stream allows both.

The following terms are roughly synonomous with "input": read, receive, extract, scan, get.

The following terms are roughly synonomous with "output": write, send, insert, print, put.

There are 2 operators one can use with a C++ stream depending on it's type:

Operator name Syntax Description
Stream insertion s << x Inserts formatted data from the value of x into the ostream named s
Stream extraction s >> x Extracts formatted data from the istream named s into the variable named x

The stream insertion and stream extraction operators convert any data to/from text data before transmitting it.

This simple code block scans 3 integer values and prints the sum of those values:

int a, b, c; cin >> a >> b >> c; cout << a + b + c << endl;

The stream extraction operator is sufficient for scanning values of type int and double; multiple input items for these types are delimited by whitespace (space, tab, newline, etc.).

When scanning a character there are two ways one can do this:

Expression Description
cin >> c Extracts the next non-whitespace character from cin and assigns it to the char variable named c
cin.get(c) Extracts the next character from cin and assigns it to the char variable named c

When scanning a string (a sequence of characters), there are two ways one can do this:

Expression Description
cin >> s Extracts the next word (a sequence of non-whitespace characters) from cin and assigns them to the string variable named s
getline(cin, s) Extracts the next line (a sequence of characters up to but not including '\n' or "\r\n") from cin and assigns them to the string variable named s

All of these Input/Output expressions return the stream variable that they operated on.

The special identifier endl is used for insertting a new line into an output stream, it is roughly equivalent to inserting the character '\n'.

One important thing you will realize is that the terminal is, by default, line oriented. So even though the stream extraction operator delimits it's input by any whitespace, data from the keyboard does not actually get sent to the program's standard input until a new line is entered (the user presses Enter).