1.4.  Standard Input and Output

[ fromfile: cppintro.xml id: stdinout ]

In Example 1.1, the directive

  #include <iostream>

enabled you to make use of predefined global input (istream) and output (ostream) objects.

  1. cin, console input, the keyboard by default.

  2. cout, console output, the console screen by default.

  3. cerr, console error, another output stream to the console screen that flushes more often and is normally used for error messages.

In Example 1.1, we made use of the global ostream object, cout. We also called one of its member functions, operator<<(). This function overloads the << operator and is used to insert data into the output stream, so we call it the insertion operator. [5] The syntax for that output statement is also quite interesting. Instead of using the rather bulky function notation,

  cout.operator<<("Factorial of: ");
  

we invoked the same function using the more elegant and readable infix syntax:

cout << "Factorial of: "; 

This operator can be chained (used on multiple values) and is predefined for use with many built-in types, as you see in the next output statement.

  cout << "The cost is $" << 23.45 << " for " << 6 << " items." << '\n';
  

In Example 1.2, you can see the operator>>() used for input with the istream object cin in an analogous way to the way we used << for output with the ostream object cout. Because the effect of this operator is to extract data from the input stream, we call it the extraction operator. [6]

Example 1.2. src/iostream/io.cpp

#include <string>
#include <iostream>

int main() {
    using namespace std;
    const int THISYEAR = 2011;  
    string yourName;
    int birthYear;

    cout << "What is your name? "  << flush;
    cin >> yourName;

    cout << "What year were you born? " ;
    cin >> birthYear;

    cout << "Your name is " << yourName
            << " and you are approximately " 
            << (THISYEAR - birthYear)
            << " years old. " << endl;
}

The symbols, flush and endl are manipulators, [7] from the std namespace.

In Example 1.2, we make use of the string class, also from the C++ Standard Library.[8] We discuss this type and demonstrate some of its functions later in Section 1.8.

[Note] For Windows/MSVC Users

By default, MSVC generates applications with no console support. This means that you cannot use the standard i/o/error streams unless you tell MSVC to build a console application. See the note [ CONFIG += console (for MSVC users) ] in Section 1.6.



[5] We discuss overloaded functions and operators further in Section 5.1. This particular operator already has a name and definition from C. It is the left shift operator. For example, n << 2 shifts the bits of the int n two positions to the left and fills the vacated bits with zeros – effectively multiplying n by 4.

[6] This time we have overloaded the right shift operator. n >> 2 shifts the bits of the int n two positions to the right, effectively dividing by 4, and fills the vacated bits appropriately depending on whether n is signed or unsigned.

[7] Manipulators are function references that can be inserted into an input or output stream to modify its state. We discuss these further in Section 1.9.

[8] We discuss classes in detail in Chapter 2. For now, you can think of a class as a data type with built-in functions.