In C++ programs, Manipulators are operators used in formatting output. More so, the data is manipulated by the programmer’s choice of display.
Here you will learn the different C++ manipulators such as endl manipulator, setw manipulator, setfill manipulator, and setprecision manipulator, as well as their general syntax
The endl manipulator has the same functionality as the ‘n’ newline character.
Example:
cout << "Programming" << endl;
cout << "Training";
The header file that must be included while using setw manipulator is displayed below:
Syntax:
setw(x)
Example:
#include <iostream>
#include <iomanip>
void main( )
{
int x1=123,x2= 234, x3=789;
cout << setw(8) << "Exforsys" << setw(20) << "Values" << endl
<< setw(8) << "test123" << setw(20)<< x1 << endl
<< setw(8) << "exam234" << setw(20)<< x2 << endl
<< setw(8) << "result789" << setw(20)<< x3 << endl;
}
Example:
#include <iostream>
#include <iomanip>
void main()
{
cout << setw(15) << setfill('*') << 99 << 97 << endl;
}
Example:
#include <iostream>
#include <iomanip>
void main( )
{
float x = 0.1;
cout << fixed << setprecision(3) << x << endl;
cout << scientific << x << endl;
}