CSE 202: Computer Science II, Winter 2018
Standard I/O

Standard I/O should have been the first thing you've seen in C++. I/O stands for Input/Output, and for C++, standard I/O involves the use of std::cin and std::cout.

Standard I/O in a more general sense, involves the use of the standard streams within a computer program. The standard streams are the main communication channels between a computer program and the rest of the operating system.

In C++, std::cin is the standard input stream and std::cout is the standard output stream. "cin" stands for "character in" and "cout" stands for "character out", this is because std::cin is typically connected to the keyboard (a device for receiving characters from the user) and std::cout is typically connected to a terminal window (a porogram for displaying characters).

These predefined objects are available when including the <iostream> header file, and they can be used with the following expressions:

Expression Meaning
std::cin >> x Extract a value from std::cin and assign it to x
std::cout << x Insert the value x into std::cout

When used with C++ streams, these operators are known as the stream insertion and stream extraction operators.

The stream extraction operator is effective for extracting values delimited by whitespace which is usually what you want when extracting values of arithmetic types.

Character and string input

For characters and strings, you may want to extract values in a different manner. For the following table, assume that s is a variable of type std::string and c is a variable of type char:

Expression Meaning
std::cin.get(c) Read the next character from std::cin and assign it to c
std::cin >> c Read the next non-whitespace character from std::cin and assign it to c
std::cin >> s Read the next word from std::cin and assign it to s
std::getline(std::cin, s) Read the next line from std::cin and assign it to s

Remember that using std::string or std::getline requires that you include <string>.

When any of these I/O expressions are used within the condition of an if statement or loop, then the result will be converted to false if the I/O operation failed.

A program can read and process all character input with the following code:

char c; while (std::cin.get(c)) { ; }

All lines can be read and processed using:

std::string line; while (std::getline(std::cin, line)) { ; }

All words can be read and processed using:

std::string word; while (std::cin >> word) { ; }

All numbers can be read and processed using:

double number; while (std::cin >> number) { ; }

For each of these cases, the user would enter CTRL-D to close standard input.

Filter programs

A filter is a type of program that reads all data from standard input and writes it's results to standard output.

Here is a simple filter program which reads all numbers entered by the user and displays the cosine of each number entered:

cos.cpp

Manipulators

A manipulator is a special function that, when used with an I/O operation, alters the way the stream behaves.

Here are a few manipulators:

Expression Meaning
std::cout << endl Inserts a new line and synchronizes the stream
std::cin >> std::ws Extracts and discards whitespace
std::cout << std::setw(10) Sets the width of the next insertion to 10

When used with an output stream, the std::setw manipulator will prepend spaces to the next value that gets inserted up to a specified length, this is useful for aligning text into columnes.

Test out the following program to see how std::setw works, modify it if you like:

setw.cpp

std::left and std::right affect how text is aligned when used with std::setw. By default, text wil bee aligned to the right.