logo

UTK Notes


I/O Manipulators and Formatting

Topics

Extraction Operator >>

  • The extraction operator takes two operands
    • The left operand is an input stream expression (like cin or an ofstream object)
    • The right operand is a variable to store input

Insertion Operator <<

  • The insertion operator takes two operands
    • The left operand is an output stream expression (like cout or an ofstream object)
    • The right operand is an expression of a simple type, or a string, or a manipulator

Manipulators

  • Manipulators are used only in input and output statements to control format
  • Manipulators are found in both <iostream> and <iomanip>
  • Examples of manipulating text
    • Allow for a “field width” to align text
    • Change how values are displayed
      • Decimal, Hex, etc.

Manipulators in <iostream>

  • endl: terminate the output line
  • fixed: display in decimal point format with 6 decimal places as default
    • --.XXXXXX
  • showpoint: display a whole number in decimal format with 6 significant digits as default
    • XX.XXXX
    • X.XXXXX
    • XXXX.XX

Manipulators in <iomanip>

  • setw(n): fieldwidth specification manipulator
    • Sets the width of the next value (int, float, string) to be printed over ‘n’ characters with right justification as default.
    • This manipulator is the ONLY one that is NOT persistent. This means it only affects the very next item displayed.
    • Useful to create/align columns in output.
  • setprecision(n): decimal number specification manipulator
    • Sets ALL subsequent floating-point outputs to be displayed with ‘n’ decimal places of accuracy provided that fixed manipulator has already been specified.
    • Default is 6 usually decimal places.
  • scientific: set ALL subsequent floating point output to be displayed in scientific notation. This is the default.
  • hex: set ALL subsequent output to be displayed in hexadecimal notation but will not place a ‘0x’ automatically in front of number.
  • dec: set ALL subsequent outputs to be displayed in decimal notation (base 10). Mainly used if you had previously set output to scientific or hex, and need to change back.
  • setfill(' '): fills the unused field width with the specified character. By default, this is a space. Useful for adding leading zeros.
  • left: set ALL subsequent outputs to left-justification within the printing field
  • right: set ALL subsequent outputs to right-justification within the printing field. This is the default, like Java’s .format().

setw() Quirks

1
cout << left << setw(15) << "Hello" << " World\n";
1
Hello     World

10 spaces (15 - 5) between Hello and World

This places Hello in a left justified field (starts on left side) with a MINIMUM 15 characters wide.

Since Hello is less than 15 characters, unused characters are filled with spaces by default.

1
cout << right << setw(15) << "Hello" << " World\n";
1
          Hello World

10 spaces (15 - 5) between Hello and World

This places Hello in a right justified field with a MINIMUM of 15 characters wide.

Again, unused characters are filled with spaces.

1
cout << left << setw(3) << "Hello" << " World\n";
1
Hello World

If you exceed setw() width, nothing gets cut off. setw() is a MINIMUM, not a maximum.

Four Formatters Example

1
2
3
4
5
6
7
8
9
10
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
  double myNumber = 123.4;
  double yourNumber = 3.14159;
  cout << fixed << showpoint << setprecision(4);
  cout << "Numbers are " << endl << setw(11) << myNumber << endl << setw(11) << yourNumber << endl;
  return 0;
}
  • Each is displayed right justified and
  • Rounded if necessary and
  • Located in a space of at least 11 positions and
  • 4 places after the decimal point
1
2
3
Numbers are
   123.4000
     3.1416

Powerpoint