Wednesday, December 28, 2011

Manipulator Functions


Manipulator Functions
Manipulator functions are special stream functions that change certain characteristics of the input and output. They change the format flags and values for a stream. The main advantage of using manipulator functions is that they facilitate that formatting of input and output streams.
     The following are the list of standard manipulator used in a C++ program. To carry out the operations of these manipulator functions in a user program, the header file input and output manipulator <iomanip.h> must be included.
 Predefined manipulators
Following are the standard manipulators normally used in the stream classes:
endl
hex, dec, oct
setbase
setw
setfill
setprecision
ends
ws
flush
setiosflags
resetiosflags
(a) Endl   the endl is an output manipulator to generate a carriage return or line feed character. The endl may be used several times in a C++ statement.
     For example,
     (1)
          cout << “ a “ << endl << “b” << endl;
     (2)
          cout << “ a = “ << a << endl;
          cout << “ b = “ << b << endl;
  A program to display a message on two lines using the endl manipulator and the corresponding output is given below.
      / / using endl manipulator
      #include <iostream.h>
      Void main (void)
      {
           cout << “ My name is Komputer “;
           cout << endl;
           cout << “ many greetings to you “;
      }
Output of the above program
    My name is Komputer
    Many greetings to you
    The endl is the same as the non-graphic character to generate line feed (\n).
    A program to illustrate the usage of line feed character and the endl manipulator and the corresponding output are given below.
     #include <iostream.h>
     void main ( )
     {
         int a;
         a = 20;
        cout << “ a = “<< a << endl;
        cout << “ a = “<< a <<   “\n”;
        cout << “ a = “<< a <<    ‘\n’;
    }
Output of the above program
    a = 20
    a = 20
    a = 20
(b) Setbase()  The setbase() manipulator is used to convert the base of one numeric value into another base. Following are the common base converters in C++.

dec   - decimal base (base = 10)
hex   - hexadecimal base (base = 16)
oct   - octal base (base = 8)
  In addition to the base conversion facilities such as to bases dec, hex and oct, the setbase() manipulator is also used to define the base of the numeral value of a variable. The prototype of setbase() manipulator is defined in the iomanip.h header file and it should be include in user program. The hex,    dec,    oct manipulators change the base of inserted or extracted integral values. The original default for stream input and output is dec.

PROGRAM 
A program to show the base of a numeric value of a variable using hex,  oct and dec manipulator functions.
     / / using dec, hex, oct manipulator
     #include <iostream.h>
     Void main (void)
{
       int   value;
       cout  << “ Enter number’ << endl;
       cin >> value;
       cout << “ Decimal base = “ << dec << value << endl;
       cout << “ Hexadecimal base = “ << hex << value << endl;
       cout << “ Octal base = “ << oct << value << endl;
    }
Output of the above program
    Enter number
    10
    Decimal base = 10
    Hexadecimal base = a
    Octal base = 12

PROGRAM
A program to show the base of a numeric value of a variable using setbase manipulator function.
    / /using setbase manipulator
    #include <iostream.h>
    #include <iomanip.h>
    void main (void)
    {
         int    value
         cout << “ Enter number” << endl;
         cin >> value;
         cout << “ decimal base = “ << setbase (10)
         cout << value << endl;
         cout << “ hexadecimal base = “ << setbase (16);
         cout << value << endl;
         cout << “ Octal base = “ << setbase (8) << value << endl;
    }
The output of this program is the same as the output of program 2.2
(c)   Setw ()  The setw ( ) stands for the set width. The setw ( ) manipulator is used to specify the minimum number of character positions on the output field a variable will consume.
     The general format of the setw manipulator function is
                       setw( int w )
Which changes the field width to w, but only for the next insertion. The default field width is 0.
    For example,
           cout << setw (1) << a << endl;
           cout << setw (10) << a << endl;
   Between the data variables in C++ space will not be inserted automatically by the compiler. It is upto a programmer to introduce proper spaces among data while displaying onto the screen.

 PROGRAM 
A program to display the content of a variable without inserting any space.
    #include <iostream.h>
    void main (void)
    {
        int   a,b;
        a = 200;
        b = 300;
        cout << a << b << endl;
    }
Output of the above program
200300

PROGRAM 
A program to insert a tab character between two variables while displaying the content onto the screen.
        / /using setw manipulator
        #include <iostream.h>
        #include <iomanip.h>
        void main (void)
        {
            int  a,b;
            a = 200;
            b = 300;
            cout << a << ‘\t’ << b << endl;
        }
Output of the above program
200     300

PROGRAM 
A program to display the data variables using setw manipulator functions.
       / /using setw manipulator
       #include <iostream.h>
       #include <iomanip.h>
       void main (void)
       {  
           int  a,b;
           a = 200;
           b = 300;
          cout << setw (5) << a << setw (5) << b << endl;
          cout << setw (6) << a << setw (6) << b << endl;
          cout << setw (7) << a << setw (7) << b << endl;
          cout << setw (8) << a << setw (8) << b << endl;
       }
Output of the above program
    200         300
      200         300
        200         300
          200         300
(d)  Setfill() The setfill ( ) manipulator function is used to specify a different character to fill the unused field width of the value.
    The general syntax of the setfill ( ) manipulator is
                     setfill( char f)
which changes the fill character to f. The default fill character is a space.
For example,
       setfill ( ‘ . ’ ) ; / / fill a dot ( . ) character
       setfill ( ‘ * ’ ) / / fill a asterisk (*) character
PROGRAM 
A program to illustrate how a character is filled in filled in the unused field width of the value of the data variable.
      / /using setfill manipulator
      #include <iostream.h>
      #include <iomanip.h>
      void main ( void)
      {
          int   a,b;
          a = 200;
          b = 300;
          cout << setfill ( ‘ * ’ );
          cout << setw (5) << a << setw (5) << b << endl;
          cout << setw (6) << a << setw (6) << b << endl;
          cout << setw (7) << a << setw (7) << b << endl;
          cout << setw (8) << a << setw (8) << b << endl;
     }
Output of the above program
    **200**300
    ***200***300
    ****200****300
    *****200*****300
(e) Setprecision()  The setprecision ( ) is used to control the number of digits of an output stream display of a floating point value. The setprecision ( ) manipulator prototype is defined in the header file <iomanip.h>.
      The general syntax of the setprecision manipulator is
                       Setprecision (int p)
      Which sets the precision for floating point insertions to p. The default precision is 6       

PROGRAM 
A program to use the setprecision manipulator function while displaying a floating point value onto the screen.
     / /using setprecision manipulator
     #include <iostream.h>
     #include <iomanip.h>
     void main (void)
     {
        float  a,b,c;
        a = 5;
        b = 3;
        c = a/b;
        cout << setprecision (1) << c << endl;
        cout << setprecision (2) << c << endl;
        cout << setprecision (3) << c << endl;
        cout << setprecision (4) << c << endl;
        cout << setprecision (5) << c << endl;
        cout << setprecision (6) << c << endl;
    }
Output of the program
    1.7
    1.67
    1.667
    1.6667
    1.66667
    1.666667

(f)  Ends  The ends is a manipulator used to attach a null terminating character (‘\0’) at the end of a string. The ends manipulator takes no argument whenever it is invoked. This causes a null character to the output.
  
PROGRAM 
A program to show how a null character is inserted using ends manipulator while displaying a string onto the screen.
      / /using ends manipulator
      #include <iostream.h>
      #include <iomanip.h>
      Void main ( )
   
  {
      int number = 231;
      cout << ‘ \ ” ’ << “ number = “ << number << ends;
      cout << ‘ \ “ ‘ << endl;
  }
Output of the above program
      “ number = 123 “
(g) Ws  The manipulator function ws stands for white space. It is used to ignore the leading white space that precedes the first field.
        / /using ws manipulator
        #include <iostream.h>
        #include <iomanip.h>
        Void main ( )
        {
            char name [100]
            cout << “ enter a line of text \n”;
            cin >> ws;
            cin >> name;
            cout  << “ typed text is = “ << name << endl;
       }
 Output of the program
        enter a line of text
        this is a text
        typed text is = this
(b)  Flush   The flush member function is used to cause the stream associated with the output to be completed emptied. This argument function takes no input prameters whenever it is invoked. For input on the screen, this is not necessary as all output is flushed automatically. However, in the case of a disk file begin copied to another, it has to flush the output buffer prior to rewinding the output file for continued use. The function flush ( ) does not have anything to do with flushing the input buffer.
        / /using flush member function
        #include <iostream.h>
        #include <iomanip.h>
        void main ( )
        {
             cout << “ My name is Komputer \n”;
             cout << “ many greetings to you \n”;
             cout . flush ( ) ;
        }
The hex, dec, oct, ws, endl, ends and flush manipulators are defined in stream.h. The other manipulators are defined in iomanip.h which must be included in any program that employs them.

(i)  Setiosflags and Resetiosflags  The setiosflags manipulator function is used to control different input and output settings. The I/O stream maintains a collection of flag bits.
       The setiosflags manipulator performs the same function as the setf function.
The flags represented by the set bits in f are set.
        The general syntax of the setiosflags is,
              setiosflags ( long h)
    The restiosflags manipulator performs the same function as that of the resetf function. The flags represented by the set bits in f are reset.
    The general syntax of the resetiosflags is a follows,
            Resetiosflags (long f) 
  
PROGRAM 

A program to demonstrate how setiosflags is set while displaying the base of a numeral.
    / /using setiosflags of basefield
    #include <iostream.h>
    #include <iomanip.h>
    void main (void)
     {
          int  value;
          cout << “ enter a number \n”;
          cin  >>  value;
          cout << setiosflags (ios : : showbase) ;
          cout << setiosflags (ios : : dec) ;
          cout << “ decimal = “ << value << endl;
          cout << setiosflags (ios : : hex) ;
          cout << hexadecimal = “ << value << endl ;
          cout << setiosflags (ios : : oct) ;
          cout << “ octal = “ << value << endl ;
      }
Output of the above program
     enter a number
     10
     decimal = 10  
     hexadecimal = 0xa
     octal = 012

No comments:

Post a Comment