If you’ve programmed in C already and used the “scanf” function, you already know what I am talking about here. Writing a float value on the screen with two digits of precision after the decimal point is done on the fly. A strong library such as C++ must have something similar implemented.

Once you’ve gone through this article, you will see that even more. You can go as far as to create your own formatters/manipulators, but that will be the subject of a different article, so stay tuned for that one.

For now, let’s see what we have implemented. Manipulators are functions that are applied with the insertion operator << in the case of output streams. They are declared in the <iostream>, <ios> or the <iomanip> header, depending on the area they are covering.

Eventually manipulators modify a couple of flags that define the behavior of the stream, defined all the way up in the ios_base class. Nevertheless, before we apply any of them, we should first grasp the knowledge of how we can reset all that we have modified.

The “flags” member function of each stream is overloaded in two ways. First, one takes no parameters and returns the current format state. The return type is a defined type inside the stream class, namely “fmtflags.” I will present my examples on the cin and cout stream connected to the console window, but they will work on any stream class. Each time you want to save it, just act as follows:

#include <iostream>

using namespace std;

int main ()

{

ostream::fmtflags basicFlag = cout.flags();

// Do manipulations on the stream // (1)

ostream::fmtflags usedFlag = cout.flags(basicFlag);

// Apply new formatting flags

cout.flags(usedFlag); // recall the format used at (1)

// Print what you want with the first collection

cout.flags(basicFlag); // reset the console stream flags

return 0;

}

This first example I’ve extended a little so you can see the header I use, and that I also added the std namespace to the global namespace, as I’m a bit too lazy to write the “std::” keyword in front of each function used.

Now we have saved in the “basicFlag” variable, in matter of effect, the current format setting applied to the cout stream. The second overload of the flags function will handle the set process. It will also return the current collection of flag settings inside the stream and set the new collection that you gave as input.

As you may expect, you do not necessarily need to save the usedFlag before the change. You could also just call it as we did in the end. We set the new settings, in fact the basic settings, saved at the start.

As far as the library where you’ll find them, most of the manipulators are found inside the iostream header; however, the ones taking an argument also will have their place in the “iomanip” header, so make sure to include that as well, once you use one of them on your stream.

All of the flag changes can be resolved with the rdstate() and setstate() member functions of streams, but this is harder to read and manage. Manipulators provide a more elegant solution, so I recommend using them; however, be aware that when using these, the changes are permanent (except one that confirms the rule :P, but we will talk about that also).

If you set a flag, it will stay until you explicitly modify it. The cin/cout streams are a prime example of this, as most programmers would like to have them working in a default mode. So in those situations where you use modifiers, make sure you delimit the modifications with a state save and a restore.

If you thought that there were already too many ways available to modify them, let me say to you that the flag sets can be done with the setf() function also, and unsetf() will turn off a flag. As in the example below; do not think about what it does, just observe the method:

cout.setf( ios_base::dec );

cout.unsetf( ios_base::dec );

In fact, the “setf” has an overload that takes two parameters, where the second is the flags that are to be turned off. This trick can save you a couple of lines when you want to turn some flags on/off at the same time.

Numeric Systems and Bool Values

In this section we will talk about how you can change the numeric system in which you display a number, but first I’ll show you a little trick for telling the cout stream to write the bool values in a more common language. Surely every programmer will know that zero is false and anything else true, but why not make that obvious to anyone?

For this, there are the boolaplha/noboolaplha flags:

cout << boolalpha<< false << ” and ” << true << noboolalpha << endl;

cout << false << ” and ” << true << endl;

false and true

0 and 1

Press any key to continue . . .

First, we printed in the more sophisticated way with the flag on. The second line (actually the third in the code snippet) is how they will get the default settings. From this you could already point out the permanent state of the flags. As noted at the end of first line, we changed the flag back to the default behavior; the noboolalpha is set on by default.

In addition, here we should cover the endl manipulator that puts in a new line and flushes the stream. You may also put just a zero char in the console if you apply the ends manipulator. Note the empty space in the example below before the “Press” word.

char a= ‘a’;

cout << a << ends;

a Press any key to continue . . .

Now how about number systems? We all know that by default, we work in the decimal number system, but sometimes we may want to express/print the number in a different one. The octal and hexadecimal systems are used near the decimal one, so only for these the basic stream manipulators offer support. For others you may write your own one.

int a = 100;

cout << oct << a << endl ;

cout << dec << a << endl;

cout << hex << a << endl;

cout << setbase(9) << a << endl;

cout << showbase<< endl;

cout << oct << a << endl ;

cout << dec << a << endl;

cout << hex << a << endl;

144

100

64

100

0144

100

0x64

Press any key to continue . . .

In the example above, I have already explained everything, but let’s go through it once again for the sake of detail. For setting a specific number in you system, you apply the corresponding manipulator to the stream: the oct stays for the octal number system, the dec for the decimal and the hex for the hexadecimal.

You can also set base explicitly though the setbase() manipulator (be warned; as this requires an argument as input, you need to include the iomanip), but calling it on anything other than 8, 10, or 16 is considered invalid input, and the decimal default will be forced. Now all that remains is learning how we tell the system in which that the number on the screen is displayed.

In order to accomplish this, the so-called showbase manipulator was made. It will display nothing in front of number if it is a decimal, add a “0” if it is in the octal system and a “0X” if the hexadecimal number system is the rule. By default, this is off.

Float Formatting

Formatting float numbers is a little more interesting, as you can change the precision of the display, the notation of the display, and even the decimal display for float values that turn out to have some whole part at a level. All of it is condensed in the lines below.

double i = 3.165; //(1)

cout << i << endl; //(2)

cout <<showpoint<< i << endl; //(3)

int old = cout.precision(2); //(4)

 

cout << i << endl; //(5)

cout << scientific << i << endl; //(6)

cout << uppercase<< i <<endl; //(7)

cout << fixed << setw(8) << right << i << endl; //(8)

cout << setw(8) << internal ; //(9)

cout << setfill(‘@’) << showpos << i << endl; //(10)

 

cout << setfill(‘#’) << showpos << -i << endl; //(11)

cout.precision(old); //(12)

3.165

3.16500

3.2

3.17e+000

3.17E+000

3.17

+@@@3.17

-3.17

Press any key to continue . . .

In the first line we just print out the number as we should—nothing devilish. In the third row of the code snippet we apply the showpoint manipulator that tells the stream what prints the zeros after the decimal points. The opposite of this is on by default, the noshowpoint manipulator.

We can set the precision of the output with the precision function of the stream. This function will return the old one, so we can restore it in the end (line 12). However, be aware that this will be the count of all numbers that will be printed, even the ones prior to the decimal point.

Here I cannot stress enough that C++ will NOT truncate the number when it converts from a larger precision to a smaller one. Instead, it will round the number, so be aware of this. Line 5 in the code and line 3 in the output demonstrate my point.

Then again there it is the scientific flag. This will display the number in a scientific manner. However, this has some additional effects. This means that the precision functions no longer set the precision for the number. Now only the after-the-decimal-point function will be applied.

Whether or not to apply the scientific mode is resolved automatically by the stream after making an evaluation of the number. If it is very large or small, the scientific view will be better for readability, so it will print it out in that fashion. Nevertheless, you can request the scientific mode with the manipulator mentioned above. If you want your output to be easy to read, better leave it on the default behavior.

If you want to print a char uppercase (also, for hexadecimal values, the X), just apply the uppercase manipulator or the nouppercase if you want the default behavior. The fixed manipulator will get the precision behavior from the scientific mode into the default settings. So just apply that to the numbers after the decimal point.

Then we have the ordering options. You have a right manipulator and a default one, left. This will make sense with the setw function. This will point out to the stream that you want that your output take n position (what you specify as an argument to the setw) on the screen.

The stream will take the output, and if it is smaller than what you specified, align it as required and fill the remaining chars as you want. By default, this character is the space, but you can modify it as I did with the setfill(‘#’) manipulator.

Here we must point out that setw is the exception function that I was talking about earlier. The setw function is special, and only affects the following first output (or input, as I will show in the last section). That is why the last line ended up printing just the number.

Other manipulators used here are the internal one, that will tell the stream to order the sign to the left and the number to the right. Then there is the showpos one, that just forces it to print the positive sign on the  screen (or inside a file).

You may ask how we turn off the fixed effects, as I have not mentioned a counterpart. The answer is that we must unset all the settings for the floatfield (all that apply for a float number) with the following method:

cout.unsetf(ostream::floatfield);

// note: or we can write a manipulator to do the same but //that will be explained in a future article

LEAVE A REPLY

Please enter your comment!
Please enter your name here