What are manipulators in C++
“Manipulators are unique functions that can be used in the input/output (statement to change a stream's format specifications.)”
Manipulators are employed to improve or format streams. There are various formats we might use when writing the data.
What do C++ manipulators do?
Simply described, manipulators are directives sent to the output stream that change the outcome in different ways. Or, to put it another way, manipulators are operators that organize the data presentation.
Example of n Manipulator in C++ for understanding:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "Hello \n my name is pooja\n";
cout << "glad to \n see you";
return 0;
}
Output:
![What are manipulators in C++](https://static.tutorialandexample.com/cpp/what-are-manipulators-in-cpp1.png)
Let's look at some of the C++ language's most frequently used manipulators.
manipulator endl
In C++, this endl manipulator is referred to as the line feed operator. This manipulator's primary function is to supply the entire line before moving the cursor to the start of the subsequent line. rather than utilizing the endl manipulator, if you want, we can utilize the n operators.
manipulator setw()
We can set the minimum field width on output with the aid of this setw manipulator.
Syntax:
setw(m)
Use of setw and endl
For the purpose of modifying the final result format, C++ manipulators are used. Additionally, C++ manipulators refer to instructions for the input/output stream object, which enables numerous modifications to the input/output format.
Example:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float normal, cat,dog,gas;
normal=89500; cat=1300; dog=4000;
gas=normal+cat+dog;
cout<<setw(10)<<"Basic"<<setw(10)<<normal<<endl
<<setw(10)<<"CAT"<<setw(10)<<cat<<endl
<<setw(10)<<"DOG"<<setw(10)<<dog<<endl
<<setw(10)<<"GAS"<<setw(10)<<gas<<endl;
return 0;
}
Output:
![What are manipulators in C++](https://static.tutorialandexample.com/cpp/what-are-manipulators-in-cpp2.png)
The total number of digits that will be shown following a decimal point can be set using the setprecision C++ Manipulators. This manipulator uses rounding to provide rounded output values. For instance, the output will show the number 57.92 in order to represent a value of 57.916 with two digits of precision.
Using C++'s setprecision Manipulators:
The following is the setprecision manipulator's general syntax:
Setprecisin(n)
Use the setprecision C++ Manipulator to show a floating-point number in an example program.
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
double n=86.7658;
cout<<setprecision(4)<<n<<endl;
cout<<setprecision(2)<<n<<endl;
cout<<setprecision(6)<<n<<endl;
cout<<setprecision(3)<<n<<endl;
}
Output:
![What are manipulators in C++](https://static.tutorialandexample.com/cpp/what-are-manipulators-in-cpp3.png)
C++'s integer manipulators
Let's check over the various manipulators that are offered now. For data types like float and integer, there are certain manipulators accessible. We offer manipulators for information of the integer type,
- Hexadecimal - will be shown if the option is selected.
- oct - it will show information in octal form.
- dec – to show information in the form of decimals.
As an illustration, if we say cout << hex << 166;
The result of the preceding statement is A6. 166 has the hexadecimal code A6 in it. So long as the manipulator is mentioned, the output will take that form. Therefore, all of the integers will be written following the manipulator in hexadecimal form. And if you wish to alter the numerical system, you must indicate decimal, octal, or whatever you choose. Please look at the example below for a better understanding.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "Hex 166: " << hex << 166 <<"\n";
cout << "Oct 166: " << oct << 166 <<"\n";
cout << "Dec 166: " << dec << 166 <<"\n";
return 0;
}
Output:
![What are manipulators in C++](https://static.tutorialandexample.com/cpp/what-are-manipulators-in-cpp4.png)
C++ Float Manipulators
Similarly, we have manipulators for floating points,
Fixed: The fixed floating-point number will display it. For instance, if you type cout fixed 173.6497, the same number will be printed.
scientific: The number will be shown either in exponent form or scientific form. For instance, if the value is cout fixed 173.6497, the scientific notation for this number, 1.736497e+02, will be shown.
Have a look at the example below for more clarity.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "Fixed Manipulator: " << fixed << 173.6497 <<endl;
cout << "Scientific Manipulator: " <<scientific << 173.6497 <<"\n";
return 0;
}
Output:
![What are manipulators in C++](https://static.tutorialandexample.com/cpp/what-are-manipulators-in-cpp5.png)
Benefits and Goals of Manipulators:
The primary purpose of it is to create the program structure.
Manipulators are used to add or remove specific special characters from streams as well as change the format of their parameter values.
Manipulators functions are unique stream functions that alter the input and output's format and properties.
iomanip.h needs to be included in order for the manipulators to function.