Define the C++ Manipulators

C++ Manipulators

C++ manipulators are used to format the output in different styles. The manipulators are the most common way to control output formatting. C++ provides different manipulators Most of these manipulators are used with cout object.

Define the C++ Manipulators

Some important manipulators in C++ are as follows:

  • endl manipulators
  • setprecision manipulators
  • setfill manipulators
  • showpoint manipulators
  • setw manipulators
  • fixed manipulators

The endl manipulator can be used by including iostream.h file in the program and setw, setprecision and setfill manipulators can be used by including iomanip.h file.

'endl' Manipulator

The word 'endl' stands for end of line. The end manipulator is used to move cursor to the beginning of the next line. It works similar to n' It does not requires any parameter.

Example

  • cout<<"Hello"<<endl<<"World";

The above line will display the following output:

  • Hello
  • World

The endl manipulator is not used as part of string. It is written with separate insertion operator (<<) without quotation marks. The above example first displays Hello. The 'endl' manipulator inserts a new line and then World' is displayed on the next line.

C++ Program to Print the Computer

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

cout<<”C”<<endl;

cout<<”O M”<<endl;

cout<<”P U”<<endl;

cout<<”T E R”<<endl;

return 0;

}

'setw' Manipulator

The word 'setw' stands for set width. The setw manipulator is used to display the value of an expression in specified columns. The value of expression can be string or number. If the value of expression is less than specified columns, the additional columns are left blank from left side. 

The output automatically uses the required columns if output is larger than the specified columns. The use of setw has no effect on the output in this case The setw manipulator is applied only to the value that is inserted after it. The output is right justified by default. The setw manipulator is part of iomanip.h

Syntax

The syntax of 'setw' manipulator is as follows
  • setw(n)

The n indicates the number of columns in which the value is to be displayed. It can be an unsigned positive integer constant, variable or expression.

C++ Program that explains the use os 'setw' manipulator:

#include <iostream.h>

#include <conio.h>

#include <iomanip.h>

void main()

{

int n = 3928;

double d = 91.5;

char str[] = "OOP using C++";

cout<<"("<<setw(5)<<n<<")"<<endl;

cout<<"("<<setw(8)<<d<<")"<<endl;

cout<<"("<<setw(16)<<str<<")"<<endl;

getch();

}

'setprecision' Manipulator

The 'setprecision' manipulator is used to set the number of digits to be displayed after decimal point. It is applied to all subsequent floating point numbers written to that output stream. The value is rounded with the use of this manipulator.

Syntax

The syntax of 'setprecision' manipulator is as follows:
  • setprecision(n)

The n indicates the number of digits displayed after the decimal point. It can be an unsigned positive integer constant, variable or expression.

C++ Program that dispalys the values of different variables using setprecision manipulator.

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
clrscr();
double r, n1 = 132.364, n2 = 26.91;
r = n1 / n2;

cout<<r<<endl;

cout<<setprecision(5)<<r<<endl;
cout<<setprecision(4)<<r<<endl;
cout<<setprecision(3)<<r<<endl;
cout<<setprecision(2)<<r<<endl;
cout<<setprecision(1)<<r<<endl;
getch();

}

'fixed' Manipulator

The 'fixed' manipulator is used to further control the output of floating-point numbers. It displays the floating-point numbers in a fixed decimal format. The following statement sets the output of floating-point numbers in a fixed decimal format.

  • cout<<fixed;

All floating-point numbers after the above statement are displayed in the fixed decimal format until the manipulator fixed is disabled. It can be disabled by using the stream member function unsetf as follows;

  • cout.unsetf(ios:fixed);

'showpoint' Manipulator

The decimal part of a floating-point number is not displayed if the decimal part of the number is zero and the user displays the number using fixed decimal format. The showpoint manipulator is used to display the decimal part even if it is zero.

The following statement sets the output numbers with a decimal point and trailing zeros on the standard input device.

  • cout<<showpoint;

 'setfill' Manipulator

The setfill manipulator is used to replace the leading or trailing blanks in the output with the specified character. It requires one parameter to specify the fill character. 

The parameter can be a character constant, character variable or an integer that represents the fill character in ASCII system. The manipulator must be used with fixed width output.

Example

The manipulator setfill ('*') or setfill(42) will replace the leading or trailing blanks in the output by The value 42 is the decimal is equivalent to the character in ASCII.

Post a Comment

0 Comments