Saturday, December 31, 2011

Functions And Program Structures


Functions And Program Structures 
·        Function definition
·        Return statement
·        Function types
·        Actual and formal arguments
·        Local and global arguments
·        Scope of variables
             Automatic variables
             Register variable
             Static variable
              External variable
·        Recursive functions
·        Preprocessors
·        Macros
·        Headers
·        Standard Functions

This chapter deals with the method of declaration of the user defined function its use in C++. The scope of the variables such as automatic, register external and static, is explained. The recursive function is described in this chapter with numerous illustrations and the additional details of the functions such as preprocessors, macros, headers and standard functions are also explained in this chapter.

 INTRODUCTION
A complex problem may be decomposed into a small or easily manageable parts or modules called functions. Functions are very useful to read, write, debug and modify complex program. They can be easily incorporated in the main program. In C++, the main ( ) itself is a function that means the main function is invoking the other functions to perform various tasks.
The main advantages of using a function are :
  • easy to write a correct small function
  • easy to read, write and debug a function
  • easier to maintain or modify such a function
  • small functions tend to be self documenting and highly readable.
  • it can be called any number of times in any place with different parameters

  Most of the high level languages such as BASIC, FORTRAN, or Pascal allow both subroutines or procedures and functions. In these languages, the formal arguments may be passed and returned only by the subroutines or procedures. The functions may take some formal arguments and can only return one value and then only through the function name itself.

        In C++, there is no difference between a function and subroutine. The functions may or may not take some formal arguments for calling portion of the program. The function may or may not transfer back, values to a called function block. It may behave like a traditional subroutine or as a function or as both. Secondly, most high level languages difference between a main program and subprograms. In C++, all modules are called functions and they all have the same structure.

Friday, December 30, 2011

Input And output (I/O) Stream Flags


Input And output (I/O) Stream Flags
To implement many of the above manipulators, I/O streams have a flag field that specifies the current settings. The flag names and their meanings are given below :



Flag Name
                        Meaning
skipws
right
internal
dec
oct
hex
showbase
showpoint
uppercase
showpos
scientific
fixed
unitbuf
stdio
skip white space during input
left justification of output
pad after sign or base indicator
decimal base (show integers in decimal format)
octal base (show integers in octal format)
hexadecimal base (show integers in hexadecimal format)
show base for octal and hexadecimal numbers
show the decimal point for all floating point numbers
show uppercase hex numbers
show ‘+’ to positive integers
use E for floating notation
use floating notation
flush all stream after insertions
flush stdout,stderr after insertion 

Arguments for setflags

First Argument (Flag Name)
            Second Argument
ios : : skipws

ios : : left
ios : : right
ios : : internal

ios : : adjustfield
ios : : dec
ios : : oct
ios : : hex

ios : : basefield
ios : : showbase
ios : : showpos
ios : : uppercase
ios : : showpoint

ios : : scientific
ios : : fixed

ios : : floatfield
ios : : unitbuf
ios : : stdio

(a)  Turning the bit format flag on   In order to change the state of the cout object, the bits that represent its state must be change. The staf ( ) function is invoked for setting the bit format flags of the I/O stream.
     The general format of the setf ( ) function is,
                    cout.set(flags to be set);
For example,
       cout.staf(ios : : showbase) ;
The bitwise OR ( | ) operator is used in the argument list of the setf( ) function in order to change the bit format flag more than one.

For example,
    cout.setf (ios : : showbase | ios : : showpoint | ios : : uppercase) ;
(b) Turning the bit flag off   The usetf ( ) function is used to change the bits directly off. This function takes exactly one argument to turn off the bit pattern.
           The general syntax of the usetf ( ) function is,
                      Cout.unsetf(flags to be turned off);
           For example,
                      cout.setf (ios : : showbase | ios : : showpoint | ios : : uppercase) ;
(b) Turning the bit format flag off  The usetf ( ) function is used to change the bits directly off. This function takes exactly one argument to turn off the bit pattern.
    The general syntax of the unstf ( ) function is,
              cout.unsetf( flags to be turned off);
  For example,
       cout.unsetf (ios : : uppercase) ;
  The bitwise OR ( | ) operator is used in the argument list in order to turn off more then one bit format flag of the I/O stream.
(c)   Basefield bit format flag   The basefielde format flag is used to display integers in the proper base.
         ios : : dec      show integers in decimal format
         ios : : oct      show integers in octal format
         ios : : hex     show integers in hexadecimal format
   Only one of the above can be set at any time. These format flags control the base in which numbers are displayed. By default, dec is set. The syntax of the basefield bit format flag setting is
      cout.setf (ios : : dec,ios : : basefield) ;
      cout.setf (ios : : oct, ios : : basefield) ;
      cout.setf (ios : : hex, ios : : basefield) ;
  PROGRAM  2.11
A program to display an integer number in different bases, namely, dec, oct, and hex using the basefield format flag setting.
     / /using basefield bit format flag
     #includ <iostream.h>
     void main ( )
     {
         int num = 71 ;
         cout << “ decimal = “ <<num << “\n”;
         cout.setf (ios : : oct, ios : : basefield);
         cout << “ octal = “ << num << “\n”;
         cout.setf (ios : : hex,ios : : basefield);
         cout << “ hexadecimal = “ << num << “\n”;
         cout.setf (ios : : dec, ios : : basefield) ;
         cout << “ decimal = “ << num << “\n”;
     }
Output of the above program
      decimal = 71
      octal = 107
      hexadecimal = 47
      decimal = 71

(d) Showbase bit format flag  The showbase format flag is used to display the base for octal and hexadecimal numbers. If showbase is set, this flag prefaces integral insertions with the base indicators used with C++ constants. If hex is set, for instance, an OX will be inserted in front of any integral insertion. This flag is not set by default. The unsetf ( ) function is invoked to undo the base setting.
The syntax of the showbase flag is,
             cout.setf( ios : : showbase);

PROGRAM 
A program to display the base of the given integer numbers using showbase flag.
    / /using showbase flag
    #include <iostream.h>
    void main ( )
    {
        int num = 71;
        cout.setf (ios : : showbase);
        cout << “ decimal = “ << num << “\n”;
        cout.setf (ios : : oct, ios : : basefield);
        cout << “ octal = “ << num << “\n”;
        cout.setf (ios : : hex, ios : : basefield);
        cout << “ hexadecimal = “ << num << “\n”;
        cout.setf (ios : : dec, ios : : basefield);
        cout << “ decimal = “ << num << “\n”;
    }
 Output of the above program
     decimal = 71
     octal = 0107
     hexadecimal = 0x47
     decimal = 71
 (e) Showpos bit format flag  The showpos format flag is used to display the sign of an integer. If this flag is set, a “+” sign will be inserted before any integral insertion. It remains unset by default. Note that on positive decimal output, the ‘+’ is assumed, but by default it will not appear. If the number is negative, the ‘-‘ sign will always appear. The unsetf ( ) function is invoked undo the setting of a positive sign.
         The syntax of the showpos flag is,
                        cout.setf(ios : : showpos);
       
PROGRAM 
A program to show the sign of the given number using the showpos flag.
     / /using showpos flag
     #include <iostream.h>
     void main ( )
     {
        int num = 71;
        cout.setf (ios : : showbase);
        cout.setf (ios : : showpos);
        cout << “ decimal = “ << num << “\n”;
        cout.setf (ios : : oct, ios : : basefield);
        cout << “ octal = “ << num << “\n”;
        cout.setf (ios : : hex, ios : : basefield);
        cout << “ hexadecimal = “ << num << “\n”;
        cout.setf (ios : : dec, ios : : basefield);
        cout << “ decimal = “ << num << “\n”;
    }
Output of the above program
    decimal = +71
    octal = 0107
    hexadecimal = 0x47
    decimal = +71
(f) Uppercase bit format flag  The uppercase bit format flag is used to display output in uppercase. By default, The following notations always appear in lowercase.
  • A hexadecimal number (a, b, c, d, e, f)
  • The base of a hexadecimal number (0x)
  • A floating point number in the scientific notation (0.3e3)
  The unsetf ( ) function is used to revert back to lowercase. The syntax of the uppercase format flag is,
                   cout.setf (ios : : uppercase);

PROGRAM 
A program to display the base of the given hexadecimal number in uppercase using the flag setting.
     / /using showpos flag
     #include <iostream.h>
     void main ( )
     {
           int num = 0x2abc;
           cout.setf( ios : : uppercase | ios : : showbase);
           cout << “ decimal = “ << num << “\n”;
           cout.setf (ios : : oct, ios : : basefield);
           cout << “ octal = “ << num << “\n”;
           cout.setf (ios : : hex, ios : : basefield);
           cout << “ hexadecimal = “ << num << “\n”;
           cout.setf (ios : : dec, ios : : basefield);
           cout << “ decimal = “ << num << “\n”;
     }
Output of the above program
    decimal = 10940
    octal = 025274
    hexadecimal = 0X2ABC
    decimal = 10940
(g)   Formatting floating point numbers  The following sections explain how floating values are formatted using the different flag setting in C++.

PROGRAM 
A program to show the floating point numbers without any special formatting.
     / /using field justification
     #include <iostream.h>
     void main ( )
     {
          float a, b, c, d ;
          a = 1.23456789 ;
          b = 34.56 ;
          c = 1.34E2 ;
          d = -123.5677 ;
          cout << “a = “ << a << “\n”;
          cout << “b = “ << b << “\n”;
          cout << “c = “ << c << “\n”;
          cout << “d = “ << d << “\n”;
Output of the above program
    a = 1.234568
    b = 34.560001
    c = 134
    d = -123.567703

(h)   Showpoint bit format flag    The showpoint bit format flag is used to show the decimal point for all floating point values. By default, the number of decimal position is six.
        The syntax of the showpoint flag is,
                       cout.setf( ios : : showpoint);
        The unsetf ( ) flag is invoked to undo the showing of all decimal values of a floating point number.

PROGRAM 
A program to display a floating point value with all decimal place.
     / /using showpoint
     #include <iostream.h>
     void main ( )
     {
         float a, b, c, d ;
         a = 1.23456789 ;
         b = 34.56 ;
         c = 1.34E2 ;
         d = -123.5677 ;
         cout.setf (ios : : showpoint);
         cout << “a = “ << a << “\n”;
         cout << “b = “ << b << “\n”;
         cout << “c = “ << c << “\n”;
         cout << “d = “ << d << “\n”;
    }
Output of the above program
     a = 1.234568
     b = 34.560001
     c = 134.000000
     d = -123.567703
(i) Precision   The precision member function is used to display the floating point value as defined by the user. The general syntax of the precision is,
               cout.precision (int n)
where n is number of decimal places of the floating value to be displayed.
     For example,
           cout.precision (5) ;
     which display a floating point number of five decimals.
   
PROGRAM 
A program to display a floating point value in the formatted from using the showpoint and precision member function.
     / /using showpoint and precision member function
     #include <iostream.h>
     void main ( )
     {
          float a, b, c, d;
          a = 1.23456789;
          b = 34.560;
          c = 134.000;
          d = -123.568;
          cout.stef(ios : : showpoint) ;
         cout.precision (3) ;
         cout << “a = “ << a << “\n” ;
         cout << “b = “ << b << “\n” ;
         cout << “c = “ << c << “\n” ;
         cout << “d = “ << d << “\n” ;
     }
Output of the above program
     a = 1.235
     b = 34.560
     c = 134.000
     d = -123.568
(j)  Floatfield bit format flag   Sometimes a floating point value may have to be displayed in scientific notation rather than in fixed format.
(i) Scientific :   When scientific is set, floating point values are inserted using scientific notation. There is only one digit before the decimal point followed by the specified number of precision digits which in turn is followed by an uppercase or a lower case e depending on the setting of uppercase and the exponent value.
    The general syntax of the scientific notation is,
                     Cout.setf( ios : : scientific, ios : : adjustfield);
(ii)  Fixed  When fixed is set, the value is inserted using decimal notation with the specified number of precision digits following the decimal point. If neither scientific nor fixed is set (the default), scientific notation will be used when the exponent is less then 4 or greater than precision. Other wise, fixed notation is used.
     The general syntax of the fixed notation is,
                   cout.setf(ios : : fixed, ios : : adjustfield);

PROGRAM 
A program to display a floating point value in both scientific and fixed notation using the floating format flag.
    / /using fixed and scientific flag
    #include <iostream.h>
   void main ( )
   {
       float a, b, c, d ;
       a = 1.23456789;
       b = 34.56;
       c = 1.35E2;
       d = -123.567 ;
       cout.setf (ios : : showpoint) ;
       cout.precision (3);
       cout.setf (ios : : fixed, ios : : floatfield) ;
       cout << “ display in conventional notation \n”;
       cout << “a = “ << a << “\n” ;
       cout << “b = “ << b << “\n” ;
       cout << “c = “ << c << “\n” ;
       cout << “d = “ << d << “\n” ;
       cout.setf (ios : : scientific, ios : : floatfield) ;
       cout << “ display in scientific notation \n”;
       cout << “a = “ << a << “\n” ;
       cout << “b = “ << b << “\n” ;
       cout << “c = “ << c << “\n” ;
       cout << “d = “ << d << “\n” ;
    }
Output of the above program
     display in conventional notation
     a = 1.235
     b = 34.560
     c = 134.000
     d = -123.568
     display in scientific notation
     a = 1.235e+00
     b = 3.456e+01
     c = 1.340e+02
     d = -1.235e+02

(k) Adjustfield bit format flag   The adjust field consists of there field setting
               ios : : left                   left justification
               ios : : right                 right justification
               ios : : internal            pad after sign or base indicator
(i) Left :   Only one of these may be set at any time. If left is set, the inserted data will be flush left in a field of characters width wide. The extra space, if any, will be filled by the fill character (specified by the fill function).
     The general syntax of the left justification format flag is,
                   cout.setf(ios : : left ,ios : : adjustfield);

(ii) Right : If right is set, the inserted data will be flush right.
     The general syntax of the right justification format flag is,
                     cout.setf(ios::right,ios::adjustfield);

(iii) Internal : If internal is set, the sign of a numeric value will be flush left while the numeric value flush right, and the area between them will contain the pad character.
    The general syntax of the internal justification format flag is,
                    cout.setf(ios : :  interal,ios : : adjustfield);

PROGRAM 
A program to demonstrate how a field justification of an integer value is carried out using the adjust field format flag.
     / /using field justification
     #include <iostream.h>
    void main ( )
    {
        int num = 71;
        cour.fill ( ‘*’ );
        cout.setf (ios : : showpos) ;
        cout.setf (ios : : left, ios : : adjustfield) ;
        cout.width(6) ;
        cout << num << “\n” ;
        cout.setf (ios : : right, ios : : adjustfield) ;
        cout.width(6) ;
        cout << num << “\n” ;
        cout.setf (ios : : internal, ios : : adjustfield) ;
        cout << num << “\n” ;
        cout.width(6) ;
    }
Output of the above program
    +71***
    ***+71
    +***71
(l)  Fill and Width  If the total number of characters needed to display a field is less then the current field width, the extra output spaces will be filled width the current fill character. In C++, it is permitted to use any character to serve as the fill character. But by default it is blank.
(i)  Fill:  The fill ( ) member function is used to specify a new fill character. Once it is specified, it remains as the fill character unless it is changed.
    The general syntax of the fill ( )  member function is,
                  cout.fill(char ch);
    Where ch is a character to be filled.
    For example,
         cout.fill ( ‘*’ ) ;
         cout.fill ( ‘0’ ) ;
(ii) Width :   The width ( ) member function is used to specify the size of the data variable.
       The general syntax of the width ( ) member function is,
                       cout.width(intn);
       where n is a total field width of a variable.
       For example,
           cout.width (10) ;
           cout.width (3) ; 
PROGRAM 
A program to demonstrate how a fill and width member function is used to display a numeral in C++.
        / /using fill and width
        #includ <iostream.h>
       void main ( )
       {  
           cout.width (10) ;
           cout.fill ( ‘x’ ) ;
           int num = 6 ;
           cout << num << “  \n” ;
           cout.width (15) ;
           cout.fill ( ‘b’ ) ;
           num = 12345 ;
           cout << num << endl ;
       }
Output of the above program
    xxxxxxxxx6
    bbbbbbbbbb12345
(m)  Unitbuf   When unitbuf is set, the stream is flushed after every insertion. This flag is unset by default.
    The general syntax of the unitbuf is,
                     cout.setf(ios : : unitbuf);
    / /using unitbuf
    #include <iostream.h>
    void main ( )
    {
        cout.setf (ios : : unitbuf) ;
        cout << “ this is a test program to flush out \n” ;
        cout << “ the input stream\n” ;
    }
(n)  Stdio  This flag flushes the stdout and stderr devices defined in stdio.h This is unset by defult.
     The general syntax of the stdio flag is,
                    cout.setf(ios : : stdio);
    / /using stdio
    #include <iostream.h>
    void main ( )
    {
        cout.setf (ios : : stdio) ;
        cout << “ this is a test program to flush out \n” ;
        cout << “ the input stream\n” ;
    }
(o)   Skipws   If set, leading white space is ignored on extraction. By default skipws is set

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