. Advertisement .
..3..
. Advertisement .
..4..
Manipulators operate to help the functions that can adjust the stream of input/output. However, we cannot modify the value of a variable and only change the stream of I/O with (<<) and (>>) operators. Manipulators are used in C++ for the output format. The programmer who has the display choice, controls the data. In this post, we will learn the C++ Manipulators such as endl, endl, setfill and setprecision. It will be clarified by the syntax and evident. Therefore, follow below message to explore and get the significant lesson of C++ Manipulators.
What are C++ Manipulators?
Manipulators are unique functions that can be used in the I/O statement to change a stream’s format specifications. The program must include the file iomanip.h in order to access manipulators. The list of C++ manipulators and their functions with examples are provided in this article.
Benefits and Goals of Manipulators
1. The primary purpose of it is to create the program structure.
2. Manipulators functions are unique stream functions that alter the input and output’s format and properties.
3. Iomanip.h needs to be included in order for the manipulators to function.
4. The insertion () and extraction (>>) operators on stream objects can be used in conjunction with manipulators functions.
5. Manipulators are used to insert or remove specific special characters from streams as well as change the format of their parameter values.
The syntax and instance about C++ the manipulators.
1. endl
Endl, which is used in the ostream, enters a new line and after reaching a new line it flushes (example, it forces all the output given on the screen, or in the file) the output stream. On the other hand, this manipulator of endle has the same functionality as the ‘n’ new line symbol.
Below is an example of endl:
1. cout << "Exforsys" << endl;
2. cout << "Training";
2. Setw
It is used to set the field width in output applications.
Syntax:
setw(x)
It leads the string or the figure to be shown with symbol x, this symbol is set up in the manipulators of setw. So, if you use setw, you need to insert the header line. For instance, this is setw’s 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;
}
Output:
test 123
exam 234
result 789
3. Setfill
Setfill is used to put the ‘c’ character on the stream’s output. So, to find the clearly this function, see below example of setfill:
#include <iostream>
#include <iomanip>
void main()
{
cout << setw(15) << setfill('*') << 99 << 97 << endl;
}
Output:
********9997
4. Setprecision
It is not only used with the figure of the floating mark, but also is to set the digit’s figure at the right of the decimal point. There are two forms in the Setprecision: fixed and scientific. However, these forms are applied when the setprecision use with the correct keywords.
The figure of the floating mark in corrected notation is printed then the keyword repaired. For scientific purposes, the figure of the floating point in a scientific item. Here is Setprecision:
#include <iostream>
#include <iomanip>
void main( )
{
float x = 0.1;
cout << fixed << setprecision(3) << x << endl;
cout << scientific << x << endl;
}
Output:
0.100
1.000e-001
At the beginning statement of cout covers the specified item, the setprecision covers argument 3. Thus, he default result is applied because the provided value is not matched.
5. Setbase
The setbase() manipulator is used to change the base of a number to a different value. The C++ language supports the following base values:
- hex (Hexadecimal = 16)
- oct (Octal = 8)
- dec (Decimal = 10)
In addition to the base converters mentioned above, Setbase () can alter a variable’s base. The foundation of input and output numbers can be changed by using the manipulators hex, oct, and dec. Let’s look at the following example:
int number = 100;
cout << "Hex Value =" << " " << hex << number << endl;
cout << "Octal Value=" << " " << oct << number << endl;
cout << "Setbase Value=" << " " << setbase(16) << number << endl;
Output
Hex Value = 0064
Octal Value = 144
Setbase Value= 0064
Conclusion
Above all, I hope our article of C++ Manipulators helps you a lot for your lesson in C++ content. If you need assistance for C++ please leave the comment in the box below. We will see you in the other lesson and discuss more in the comment box. Finally, we wish you a successful day and remember to follow our articles to learn more about programming.
Read more
Leave a comment