ostream Output

Requires #include <ostream> ; or #include <iostream> preprocessor directive.

 

The insertion operator << is used to send information to the output stream.

 

Can be overloaded for specific purposes (e.g. for an object of a specific class data type).

 

A few problems may arise that can overcome by the use of flags and manipulators:

  • setf(parameter) - used to set the flag as indicated by the parameter
  • unsetf(parameter) - turns off the flag as indicated by the parameter

 

Flags live in the ios class, manipulators lives in the std namespace, and the member functions live in the ostream class.

 

  • Flags - used within setf() of unsetf()
    • (ios::boolaplpha) - Prints "true" of "false" boolean values
    • (ios::noboolapha) - Prints 1 or 0 boolean values (default
    • (ios::showpos) - Prefix positive numbers with a +
    • (ios::noshowpos) - Don't show positive numbers with a +
    • (ios::uppercase) - Use upper case letters
    • (ios:: dec, ios::basefield) - Prints values in decimal (default)
    • (ios:: hex, ios::basefield) - Prints values in hexadecimal
    • (ios:: oct, ios::basefield) - Prints values in octal
  • Manipulators
    • boolaplpha - Prints "true" of "false" boolean value
    • noboolapha - Prints 1 or 0 boolean values (default)
    • showpos - Prefix positive numbers with a +
    • noshowpos - Don't show positive numbers with a +
    • uppercase - Use upper case letters (for dec, hex, oct, scientific notation - not strings!)
    • nouppercase - Use lower case letters
    • dec - Prints values in decimal (default)
    • hex - Prints values in hexadecimal
    • oct - Prints values in octal
    • fixed - Use decimal notation for values
    • scientific - Use scientific notation for values
    • showpoint - Show a decimal point and trailing zeroes for floating point values
    • noshowpoint - Don't show a decimal point and trailing zeroes for floating point values
    • setprecision(number) - Sets the precision to 'number' for floating point numbers
    • internal - Left justifies the sign and right justifies the number
    • left - Left justifies the sign and number
    • right - Right justifies the sign and number
    • setfill(character) - Set the specified character parameter as the fill character
    • setw(number) - Sets the field width for input and output of the specified number parameter
  • Methods
    • precision() - Returns the current precision of floating point numbers
    • precision(number) - Sets the precision of floating point numbers to 'number' and returns the previous precision
#include <iostream>
#include <iomanip>
using namespace std;

int main () {

	int number;

	cout << "Please enter an integer:" << endl ;
	cin >> number ;

	cout.setf(ios::showpos); //turn on the ios::showpos flag
	cout << "Notice the + symbol now being printed: " << number << endl ;

	cout.unsetf(ios::dec); //turn off decimal output
	cout.setf(ios::oct) ; //turn on octal output
	cout << "Now printing in octal: " << number << endl ;

	cout.unsetf(ios::oct); //turn off octal output
	cout.setf(ios::hex) ; //turn on hexadecimal output
	cout << "Now printing in hex: " << number << endl << endl ;
	cout.unsetf(ios::hex); //turn off hex output

	cout << "Now using manipluators!" << endl ;
	cout << dec << number << endl ;
	cout << oct << number << endl ;
	cout << hex << number << endl ;

	cout << "Booleans: " << endl ;
	cout << boolalpha << true << " " << false << endl ;
	cout << noboolalpha << true << " " << false << endl ;

	return 0;
}

Compile & Run:

Please enter an integer:
42
Notice the + symbol now being printed: +42
Now printing in octal: 52
Now printing in hex: 2a 

Now using manipluators!
+42
52
2a
Booleans:
true false
1 0

Leave a Reply