Tutorials
C++In this C++ tutorial, you will learn what a manipulator is, endl manipulator, setw manipulator, setfill manipulator and setprecision manipulator explained along with syntax and examples.
Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer’s choice of display.
There are numerous manipulators available in C++. Some of the more commonly used manipulators are provided here below:
This manipulator has the same functionality as the ‘\n’ newline character.
For example:
|
produces the output:
Exforsys
Training
This manipulator sets the minimum field width on output. The syntax is:
setw(x)
Here setw causes the number or string that follows it to be printed within a field of x characters wide and x is the argument set in setw manipulator. The header file that must be included while using setw manipulator is <iomanip.h>
|
setw(8) setw(20)
Exforsys Values
E1234567 12345
S1234567 23456
A1234567 7892
This is used after setw manipulator. If a value does not entirely fill a field, then the character specified in the setfill argument of the manipulator is used for filling the fields.
|
The output of the above program is
$$$$$$$$5033
This is because the setw sets 10 width for the field and the number 50 has only 2 positions in it. So the remaining 8 positions are filled with $ symbol which is specified in the setfill argument.
The setprecision Manipulator is used with floating point numbers. It is used to set the number of digits printed to the right of the decimal point. This may be used in two forms:
These two forms are used when the keywords fixed or scientific are appropriately used before the setprecision manipulator. The keyword fixed before the setprecision manipulator prints the floating point number in fixed notation. The keyword scientific before the setprecision manipulator prints the floating point number in scientific notation.
|
The above gives ouput as:
0.100
1.000000e-001
The first cout statement contains fixed notation and the setprecision contains argument 3. This means that three digits after the decimal point and in fixed notation will output the first cout statement as 0.100. The second cout produces the output in scientific notation. The default value is used since no setprecision value is provided.
|
how to write a star programme ...in all manne i mean reverse star left hand side star and ..all position of star ...eg... * * * * * * * * * * * * * * * pls help ...it is used through manipulators |
|
use loops... #include using namespace std; int main() { int x,y; for(x=1;x |
| how to code using manipulators in c++ program |
| Why we use namespace std in beginning of C++ programme? |
| Why do we use namespace std in beginning of C++ programme? |
|
because if you don't use namespace std, you will have to use example. std::cout<<"Example"; with the namespace std there is no need for std::. |
|
#include<iostream.h> #include<iomanip.h> int main() { for (int a = 0, b = 1; a < 10; a++) { cout << endl; for (;;) { cout << setfill('*') << setw(b++) << '*'; if (a < 10) break; } } cout << endl; return 0; } |