Page 272 - DCAP404 _Object Oriented Programming
P. 272

Unit 12: Console I/O




               n = 14                                                                           Notes
               p = 175
          2.   The setw manipulator does the job as follows:
               cout      <<setw(5) <<sum <<endl;

               The manipulator setw(5) specifies a field width 5 for printing. The value of the variable
               sum is printed as 5 digits. This value is right justified within the field.

                               3      4      5

          We can also write our own manipulators as:
          #include <iostream.h>

          ostream & Symbol (ostream & output)
          {
                 return  output  <<“\+`”;
          }
          The symbol is the new manipulator, which represents ` The identifier symbol can be used

          Wherever we need to display the string “`”.
          12.4.1 Manipulate Function


          Manipulators are special functions that can be included in the I/O statements to alter the format
          parameters of a stream. The most commonly used manipulators functions are given below. To
          access these function, we must include the file iomanip.h in the program.
          1.   Setw (int w): As discussed earlier, this manipulator function is used to changes or set the
               field width for output to w.

          2.   Set fill (char C): It is used to change or set the fill character to n (default is a space).
          3.   Set precision (int p): It is used to change or set the floating point precision to p.
          4.   Set base (base n): It is used to changes base to n, where n is 8, 10, or 16. If n is zero, output
               is base 10, but input uses the C convention: 10 is 10, 010 is 8 and O x c is 12.
          5.   Set Pos flags (print flags f): This manipulator function changes or sets the format flag f.
               Setting remain in effect until next change.

          6.   Reset i os flags (fmt flags f): This manipulators clears only the flags specified by f. Setting
               remains in effect until next change.

          12.4.2 Predefined Manipulators

          Manipulators comes in tow flavors: Those take an argument as discussed in previous topic and
          those that don’t. Manipulators with no arguments are known as predefined manipulators. These
          are provided in iostream.h. The most commonly used predefined manipulators are given below:

          1.   end l; This manipulator sends a newline to the stream and flushes it.
          2.   skipws: It is used to skip white space on input.






                                           LOVELY PROFESSIONAL UNIVERSITY                                   265
   267   268   269   270   271   272   273   274   275   276   277