C / C++ / C#

  1. Home
  2. Computing & Technology
  3. C / C++ / C#

Learn About Input and Output

By David Bolton, About.com

2 of 8

Using cout to format output

The object cout is a member of the iostream library. Remember that this has to be included with a
#include <iostream>
This library iostream is derived from ostream (for output) and istream for input.

Formatting Formatting of text output is done by inserting manipulators into the output stream.

What is a Manipulator?

It's a function that can alter the characteristics of the output (and input) stream. On the previous page we saw that << was an overloaded function that returned a reference to the calling object e.g. cout for output or cin for input. All manipulators do this so you can include them in the output << or input >>. We'll look at input and >> later on in this lesson.
count << endl;
endl is a manipulator which ends the line (and starts a new one). It is a function that can also be called in this way.
endl(cout) ;
Though in practice you wouldn't do that. You use it like this.
cout << "Some Text" << endl << endl; // Two blank lines

Files are just Streams

Something to bear in mind that with much development these days being done in GUI applications, why would you need text I/O functions? Isn't that just for console applications? Well you will probably do file I/O and you can use them there as well but also what is output to screen usually needs formatting as well. Streams are a very flexible way of handling input and output and can work with
  • Text I/O. As in console applications.
  • Strings. Handy for formatting.
  • File I/O.

Manipulators Again

Although we have been using the ostream class, it is a derived class from the ios class which derives from the ios_base. This ancestor class defines the public functions which are manipulators.

On the next page List of Functions and Manipulators.

Explore C / C++ / C#

About.com Special Features

C / C++ / C#

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. Formatting with cout

©2009 About.com, a part of The New York Times Company.

All rights reserved.