C / C++ / C#

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

Learn About Input and Output

By David Bolton, About.com

4 of 8

Examples Using cout

// ex2_2cpp

#include "stdafx.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
cout.width(10) ;
cout << right << "Test" << endl;
cout << left << "Test 2" << endl;
cout << internal <<"Test 3" << endl;
cout << endl;
cout.precision(2) ;
cout << 45.678 << endl;
cout << uppercase << "David" << endl;
cout.precision(8) ;
cout << scientific << endl;
cout << 450678762345.123 << endl;
cout << fixed << endl;
cout << 450678762345.123 << endl;
cout << showbase << endl;
cout << showpos << endl;
cout << hex << endl;
cout << 1234 << endl;
cout << oct << endl;
cout << 1234 << endl;
cout << dec << endl;
cout << 1234 << endl;
cout << noshowbase << endl;
cout << noshowpos << endl;
cout.unsetf(ios::uppercase) ;
cout << hex << endl;
cout << 1234 << endl;
cout << oct << endl;
cout << 1234 << endl;
cout << dec << endl;
cout << 1234 << endl;
return 0;
}
The output from this is below, with one or two extra line spaces removed for clarity.
Test
Test 2
Test 3
46
David

4.50678762E+011

450678762345.12299000

0X4D2
02322
+1234

4d2
2322
1234
Note: Despite the uppercase, David is printed as David and not DAVID. This is because uppercase only affects generated output- e.g. numbers printed in hexadecimal. So the hex output 4d2 is 4D2 when uppercase is in operation.

Also, most of these manipulators actually set a bit in a flag and it is possible to set this directly with

cout.setf()
and clear it with
cout.unsetf()

On the next page : More About setf And unsetf()

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. Examples Using cout

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

All rights reserved.