Standard Input / Output


Standard output
Insertion operator (<<)
#include <iostream>
...
cout << "one" << "two" << endl;

Formatting output
Setting and unsetting options
  • ios::setf()
  • ios::unsetf()
Options
  • ios::skipws, ios::left, ...
Manipulators
Zero parameters
  • endl // Endline
  • ends // End-of-string
  • dec, oct, hex
  • flush
  • ws // Skip whitespace
One parameter
  • setw(x) // Set field width
  • setfill(x) // Set fill char
  • setprecision(x) // Number of digits after the decimal point
  • setiosflags(x), resetiosflags(x)
Examples
#include <iostream>
...
cout << dec << 123 << hex << 123 << endl;


cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
cout << 12.3456 << endl;

Standard input
Extraction operator (>>)
#include <iostream>
...
cin >> x >> y;
get / getline
#include <iostream>
...
cin >> get(ch) >> get(str, length);
cin.getline(str, length);


// Read each line.
while (!cin.getline(str, length).eof()) { ... }


// Test for input error.
if (!cin) { ... }
if (cin.fail()) { ... }
if (!cin.good()) { ... }