Several Data Types

The I/O Manipulators

In addition to setting or clearing the format flags directly, you can alter the format parameters of a stream through the use of special functions called manipulators, which can be included in an I/O expression. The standard manipulators are shown in the following table:

Manipulator

Purpose

Input/Output

boolalpha

Turns on boolapha flag

Input/Output

dec

Turns on dec flag

Input/Ouput

endl

Output a newline character and flush the stream

Output

ends

Output a null

Output

fixed

Turns on fixed flag

Output

flush

Flush a stream

Output

hex

Turns on hex flag

Input/Output

internal

Turns on internal flag

Output

left

Turns on left flag

Output

nobooalpha

Turns off boolalpha flag

Input/Output

noshowbase

Turns off showbase flag

Output

noshowpoint

Turns off showpoint flag

Output

noshowpos

Turns off showpos flag

Output

noskipws

Turns off skipws flag

Input

nounitbuf

Turns off unitbuf flag

Output

nouppercase

Turns off uppercase flag

Output

oct

Turns on oct flag

Input/Output

resetiosflags(fmtflags f)

Turn off the flags specified in f

Input/Output

right

Turns on right flag

Output

scientific

Turns on scientific flag

Output

setbase(int base)

Set the number base to base

Input/Output

setfill(int ch)

Set the fill character to ch

Output

setiosflags(fmtflags f)

Turn on the flags specified in f

Input/Output

setprecision (int p)

Set the number of digits
of precision

Output

setw(int w)

Set the field width to w

Output

showbase

Turns on showbase flag

Output

showpoint

Turns on showpoint flag

Output

showpos

Turns on showpos flag

Output

skipws

Turns on skipws flag

Input

unitbuf

Turns on unitbuf flag

Output

uppercase

Turns on uppercase flag

Output

ws

Skip leading whitespace

Input

To use a manipulator that takes a parameter, you must include <iomanip>.

Programming Tip 

One of the most interesting format flags found in the modern iostream library is boolalpha. This flag can be set either directly of by using the manipulators boolalpha() of noboolalpha(). What makes boolalpha so interesting is that setting it allows you to input and output Boolean values using the keywords true and false. Normally, you must enter 1 for true and zero for false. For example, consider the following program:

// Demonstrate boolalpha format flag. #include <iostream> using namespace std; int main () {   bool b;   cout << "Before setting boolalpha flag: ";   b = true;   cout << b << " ";   b = false;   cout << b << endl;   cout << "After setting boolapha flag: ";   b = true;   cout << boolalpha << b << " ";   b = false;   cout << b << endl;   cout << "Enter a Boolean value: ";   cin >> boolalpha >> b;   cout << "You entered " << b;   return 0; } 

Here is a sample ren:

Before setting boolalpha flag: 1 0 After setting boolalpha flag: true false Enter a Boolean value: true You entered true

Once the boolalpha flag has been set, Boolean values are input and output using the words true or false. As the program shows you must set boolalpha flag for cin and cout separately. Like all format flags, setting boolalpha for one stream does not imply that it is also set for another.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net