[ 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.
cin
, console input, the keyboard by default.
cout
, console output, the console screen by default.
cerr
, console error, another output stream to the console screen that flushes more often and is normally used for error messages.
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: ";
cout << "The cost is $" << 23.45 << " for " << 6 << " items." << '\n';
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,
[2] from the std
namespace.
In Example 1.2, we make use of the string
class, also from the C++ Standard Library.[3]
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 [ |
[2] 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.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |