Get to know io Manipulation of C++ ?

This post is for those who admire programming 'C++'.

Does anybody remember how we added some style & kind of a formatting to the out put of basic C++ programs? We called it, 'Manipulating' in books.

Today, mistakenly I hit back on the previous lessons. Which is kind of strange to know how far we've come through ?

'iomanip' is a Standard C++ Library Header File which you may need to include in your program in order to use them.'iomanip' consists some functions that I thought I should have a special note for.

following program includes 3 of the functions that this post will be explaining, namely:
  1. setw(n)
  2. setiosflags()
  3. resetiosflags()

#include<iostream>
#include<iomanip>
using namespace std;
void set(int num);void setFirstRow();

int main()
{
        setFirstRow();
        set(10);
        return 0;
}

void set(int num)
{
        while(num!=0)
        {
                cout<<setw(10)<<num;
                cout<<setw(10)<<oct<<setiosflags(ios_base::oct)<<num;
                cout<<resetiosflags(ios_base::oct)<<endl;
                num--;
        }
}

void setFirstRow()
{
        cout<<setw(10)<<"Value dec";
        cout<<setw(10)<<"Value oct"<<endl;
}                                                                                                                                            


Take a look at the : 

setw(n)

It causes the values to be printed in a stream which is n characters wide.

setiosflags(ios_base::oct) 

causes the number format to be changed into Octal & 

resetiosflags(ios_base::oct) 


reverses that.

   4.  setprecision(n) - causes the output to format decimal numbers to limit the decimal places. 'n'  signifies the total amount of character of the decimal number without '.' value.


#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
        double a= 32.33321;

        cout<<setprecision(5)<<a<<endl;

        return 0;
}                 

The output is 32.333 as the precision number is set to 5.


That's it for this post, Enjoy C++!

No comments: