Manipulators

Default Image

We use manipulators to format the output
on screen in a specific manner. Manipulators are the operator for
formatting. With the help of manipulators programmer can choose how to
display output on screen or device. The manipulators that are used with
arguments are worked as functions returning objects of undetermined
type. These manipulators define their own operator<< or
operator>> which perform the requested manipulation. To use any
manipulator other than “endl” we must include new header file . they can
be used for both input and output. Few of these manipulators are as
give below:


Manipulators defined in header













































boolalpha


noboolalpha

switches between textual and numeric representation of Booleans
showbase


noshowbase

controls whether prefix is used to indicate numeric base
showpoint


noshowpoint

controls whether decimal point is always included in floating-point representation
showpos


noshowpos

controls whether the + sign used with non-negative numbers
skipws


noskipws

controls whether leading whitespace is skipped on input
uppercase


nouppercase

controls whether uppercase characters are used with some output formats
unitbuf


nounitbuf

controls whether output is flushed after each operation
internal


left


right

sets the placement of fill characters
dec


hex


oct

changes the base used for integer I/O
fixed


scientific


hexfloat


defaultfloat

changes formatting used for floating-point I/O

Defined in header





















ws Consume white space
ends Outputs ’\0’
flush Flushes the output stream
endl move cursor to new line and flushes the output stream.

Defined in header

















































resetiosflags clears the specified ios_base flags
setiosflags sets the specified ios_base flags
setbase changes the base used for integer I/O
setfill changes the fill character
setprecision changes floating-point precision
setw changes the width of the next input/output field
get_money parses a monetary value
put_money formats and outputs a monetary value
get_time parses a date/time value of specified format
put_time formats and outputs a date/time value according to the specified format
quoted inserts and extracts quoted strings with embedded spaces

1.1.1        endl Manipulator


It acts as a stream manipulator which is used to feed the whole line
and then point the cursor to the beginning of the next line. It works
same as \n. Let’s see how both are used:


cout<<“This is first syntax \n”;


cout<<“This is first syntax”<

Both above code example show same result and cursor will moved to the start of the next line.


1.1.2        setw Manipulator


It is used to set minimum width for output. By default, output is
displayed right-justified within specified field. Syntax of setw is
given below:


setw(n)


Few examples of set(w) are given below:















































Code Output

cout<


c o m p u t e r



4 5

cout<
t h i s
i s
s y n t a x 3 5

set_width_output


1.1.3        setfill Manipulator


It is used to fill starting blank spaces in the output with a
specified character. It is used with the combination of setw
manipulator. Let’s show you some example:


cout<

 






























1 2 3 4 5 6 7 8 9 10 11 12
# # # G o o d
l u c k

Read Count: 3 times

Comments

No comments yet.

Leave a Comment