Understanding C++ Manipulators (UCM)

Manipulators are generally used to adjust the display of data. For example, to set a value to be displayed with a width of 10 characters and set the right margin to that width.

1. setw()

The setw() manipulator is useful for setting the width of a data display. For more details, see the following program:

#include <iostream.h>
#include <iomaip.h>
#include <conio.h>

/**
*bundet.com
*Manipulator dengan fungsi setw()
*/

void main() {
int jumbar1 = 150, jumbar2 = 23, jumbar3 = 1401;
cout << "Barang 1 = " << setw(4) << jumbar1 << endl;
cout << "Barang 2 = " << setw(4) << jumbar2 << endl;
cout << "Barang 3 = " << setw(4) << jumbar3 << endl;
getch();
}

Execution result:

Barang 1 = 150
Barang 2 = 23
Barang 3 = 1401

By using setw(), the execution result is seen to be right aligned. This can be distinguished from the execution result in example 3.2 which is left aligned.

2. setfill()

The setfill() manipulator is useful for setting the characters used to fill a specified field section  setfill(), which is not used to display data.

Example program:

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

/**
*bundet.com
*Manipulator dengan fungsi setfill()
*/

void main() {
float harga = 123.45 clrscr();
cout << " setw(8) << harga << endl;
cout << setfill('*') ;
cout << setw(8) << harga << endl;
getch();
}

Execution result:

  123.45
**123.45

In the program above 123.45 is displayed with a width of 8 characters and preceded by 2 space characters. If there is a statement  setfill('*') then the 2 space characters are filled by the character  * .

3. setiosflag()

The setiosflag() manipulator is a manipulator that can be used to control a number of format flags listed in the following table:

| Tanda Format    | Keterangan                                                                          |
|-----------------|-------------------------------------------------------------------------------------|
| ios::left       | Menyetel rata kiri terhadap lebar field yang diatur melalui setw()                  |
| ios::right      | Menyetel rata kanan terhadap lebar field yang diatur melalui setw()                 |
| ios::scientific | Memformat keluaran dalam notasi eksponensial                                        |
| ios::fixed      | Memformat keluaran dalam bentuk notasi desimal                                      |
| ios::dec        | Memformat keluaran dalam basis 10 (desimal)                                         |
| ios::oct        | Memformat keluaran dalam basis 8 (oktal)                                            |
| ios::hex        | Memformat keluaran dalam basis 16 (heksadesimal)                                    |
| ios::uppercase  | Memformat huruf pada notasi heksadesimal dalam bentuk huruf kapital                 |
| ios::showbase   | Menampilkan awalan 0x untuk bilangan heksadesimal atau 0 (nol) untuk bilangan oktal |
| ios::showpoint  | Menampilkan titik desimal pada bilangan pecahan yang tidak memiliki bagian pecahan  |
| ios::showpos    | Untuk menampilkan tanda + pada bilangan positif                                     |

4. setprecision()

If you are working with fractional numbers, you also set the number of fractional digits to display. This can be done using the setprecision() manipulator. Its form is:

setprecision(n)

with n representing the number of desired fractional digits. For example:

cout << setprecision(2) << 123.56789 << endl;

will display:

123.57

The fractional part consists of two digits according to the argument in setprecision().

Example Program:

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>

/**
*bundet.com
*Manipulator dengan fungsi setprecision()
*/

void main() {
float nilai = 123.45;
cout << setiosflags(ios::fixed);
cout << setprecision(0) << nilai << endl;
cout << setprecision(1) << nilai << endl;
cout << setprecision(2) << nilai << endl;
cout << setprecision(3) << nilai << endl;
cout << setprecision(4) << nilai << endl;
cout << setprecision(5) << nilai << endl;
cout << setprecision(6) << nilai << endl;
cout << setprecision(7) << nilai << endl;
getch();
}

Execution result:

123
123.4
123.45
123.450
123.4500
123.45000
123.449997
123.4499969

Meanwhile, below shows the results if setw() is used but ios::fixed is not included.

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>

/**
*bundet.com
*Menunjukkan efek setprecision()
*Tanpa tanda format ios::fixed
*/

void main() {
float nilai = 123.45;
cout << setprecision(0) << nilai << endl;
cout << setprecision(1) << nilai << endl;
cout << setprecision(2) << nilai << endl;
cout << setprecision(3) << nilai << endl;
cout << setprecision(4) << nilai << endl;
cout << setprecision(5) << nilai << endl;
cout << setprecision(6) << nilai << endl;
cout << setprecision(7) << nilai << endl;
getch();
}

Execution result:

le+02
le+02
1.2e+02
123
123.4
123.45
123.45
123.45

Hope this is useful & happy learning!

Source:

© STMIK El Rahma Yogyakarta. Compiled by Mr. Eko Riswanto, ST, M.Cs,


Post a Comment

Previous Next

نموذج الاتصال