Few programs are useful without the ability to input the data upon which they operate and output those operations' results. Modern computers support input and output through an interactive graphical user interface (GUI). However, older systems were strictly text-based (CLI). For simplicity, we begin our discussion with the text-based I/O system called the console.
The most basic C++ input and output (I/O) operations are to and from the console. The first console was a teletype machine, which was originally developed for and used to send messages similar to today's e-mail. The Cathode ray tube (CRT) eventually replaced the teletype as a computer's primary output device. Initially, CRTs could display text but not windows or, until much later, graphics.
Although the console is not as appealing or intuitive as a GUI, the console remains an important user interface (UI). On modern systems, the console takes one of two forms. The first form is just as it appeared in the pre-GUI days. We often see this console version before the operating system starts the GUI. This version is also the only interface available on some server operating systems (Ubuntu Server, e.g.) that omit the GUI to avoid its overhead. Windows users' systems will see this console if they pres F8 early in the boot process or if the system encounters a problem while shutting down. We'll see the second form when running C++ programs from an IDE. Some IDEs automatically open a text-based window when running a program; other IDEs provide the same text-based I/O operations in a panel displayed in the IDE window itself. Users can also open a console window directly by running one of many console programs: command prompt, power shell, bash (Bourne again shell), etc. This section introduces several C++ features that support console I/O and related concepts.
The C++ library contains several stream objects through which it can perform console I/O operations. The I/O objects represent data as streams or sequences of bytes (i.e., characters) as they enter or leave the running program. The following figure illustrates the three most common stream objects: cin
, cout
, and cerr
. The "c" appearing in the name of each object stands for "console." All console objects send data to and receive data from the console. A modern console consists of the keyboard and the screen or the console window when running a GUI. As depicted in the illustration below, input comes from the keyboard through cin
, and "normal" output is sent to the screen through cout
. Programs often send error messages or diagnostics to the screen through cerr
. C++ uses two output streams so that the user can separate them with console operators and send the two streams to different destinations when desired.
cin
is an instance of a class named istream, while cout
and cerr
are instances of of a class named ostream. C++ saves the three stream objects in a library, and the linker/loader extracts them and incorporates them in programs as needed. Whenever our program needs to use a library feature, the program must describe it, or, more formally, declare it. We typically supply this information by #including a header file at the top of the C++ source code file.
#include <iostream> using namespace std; . . . cout << "hello world" << endl; |
#include <iostream> . . . std::cout << "hello world" << std::endl; |
(a) | (b) |
cin
, cout
, and cerr
are stream objects, supporting numerous functions. Programs access many of those functions with the dot operator just like the methods in a Java program. However, other functions have a rather unusual syntax that is quite different from Java's. These special functions take one of two forms: operators or manipulators.
C++ has another feature that has no analog in Java: overloaded operators. Operators are, as we will see in greater detail later, just functions, but they are functions that support a novel calling syntax. Most of our initial experience with operators center on the <<
(inserter or output) operator and the >>
(extractor or input) operator. These two operators work in conjunction with cout
, cerr
, and cin
to write data out from or read data into a program:
The example above also uses a manipulator. Manipulators are also special functions designed to work with either the <<
(inserter) or the >>
(extractor) operator. The endl
manipulator (the last character is a lower-case 'L') inserts an end-of-line and flushes the output buffer (i.e., it forces the output stream to send the buffer's contents to the console even if the buffer isn't full). The end-of-line causes the cursor (a symbol indicating where the console will display the next character) to drop down one line and return to the left side of the screen (i.e., console window). The following example demonstrates the endl
manipulator:
Note:
If you remember that endl is short for end-line, it's easy to remember that the last character in endl is a lower-case 'L'Sometimes, when writing programs, especially when sending output to the screen, programmers often need to reference characters without a clear, visible representation or characters with special meaning within a program. Both C++ and Java use a special sequence of characters, called an escape sequence, to denote these characters and distinguish them from other programming elements. Each of the escape sequences in the following table begins with \
, which is called the escape character. Note that different devices may respond differently to a specific escape sequence.
Sequence | Character | Description |
---|---|---|
\n |
Newline | The newline or ASCII linefeed character; similar in effect to endl but doesn't flush the output buffer;cout << "Hello world\n"; |
\a |
Alert | Makes an audible sound |
\b |
Backspace | Moves the cursor back one space |
\f |
Form-feed | Also known as a page separator |
\r |
Carriage Return | Similar to the newline on some systems |
\t |
Tab | Horizontal tab |
\\ |
Backslash | Print a \ character: cout << "\\" << endl; |
\' |
Single Quotation Mark | Make a character out of a single quotation mark: '\'' |
\" |
Double Quotation Mark | Embed a double quotation mark in a string: cout << "I said, \"Hello!\"" |
\xdd |
Hexadecimal Character | Used to form any 8-bit character; often used to represent control or extended-ASCII characters:
'\x0A' is the newline character, '\x41' is a upper-case 'A', and
'\x9C' is the British pound symbol: £ |
'\\'
represents a single backslash character.