Output in C++ can be fairly simple.
We have cout, which is "standard output", actually a predefined instance of the ostream class.
To write output to cout, we use the insertion operator <<. The name refers to "inserting values into the output stream".
If we have variables such as
int M = 13; float G = 5.91; char X = 'P';
we can simply write
cout << M;
and the value of M is printed. We could string several of these together and include strings:
cout << "M = " << M << " and " G = " << G;
In each case, the value of the string or variable is printed using a default format. What if we want to change this and print values in other formats?
C++ provides a family of format specifiers we can use.
A few specifiers useful in general
This the end-of-line marker, like "\n" in the C language. It advances to the begining of the next line. It also flushes the output buffer; that is, it makes sure anything waiting to be printed is printed before finishing the current line.
This simply flushes the output buffer.
This sets the field width. That is, if we have a value to print, we can use setw to dictate the minimum number of positions to use. If the value is too big to fit, more positions will be used.
This affects only the next value to be printed. It has no lingering effect.
Without setw, values are normally printed using however many positions are needed.
By default, values are right-justified in the space provided. We can change this.
When we use setw, we can use this to force values to be printed left-justified in the space provided. This stays in effect until we change it.
When we use setw, we can use this to force values to be printed right-justified in the space provided. This stays in effect until we change it.
This is the default, but we may need it to undo the effect of left.
This sets the value of the fill character. The default value is a blank.
For instance, if we have an int variable J with the value 23, we might want to print it using exactly 6 spaces. In that case, we would have several copies of the fill character before printing the 23.
Formats for number in general
When printing a nonnegative integer value, indicate the sign with a prefix '+'.
For negative integer values, we always have a leading '-' to indicate the sign.
This switches off the effect of showpos.
This is the default, but we might need it to undo the effect of showpos.
When printing number in base 16 (the hex option) or in scientific notation, use upper-case letters. Thus we might print "4.73E12" instead of "4.73e12" and "X659" instead of "x659".
This switches off the effect of uppercase.
This is the default, but we might need it to undo the effect of uppercase.
Formats for integers
This causes integer values to be printed in base 8. This stays in effect until we change it.
This causes integer values to be printed in base 16. This stays in effect until we change it.
This causes integer values to be printed in base 10. This stays in effect until we change it.
This is the default, but we might need it to undo the effect of hex or oct.
When printing an integer value, indicate the base used (8, 10 or 16.
Base 8 is indicated by a prefix '0', as in 0123, and base 16 is indicated by a prefix by 'x', as in x123.
This stays in effect until we change it.
This switches off the effect of showbase.
This is the default, but we might need it to undo the effect of showbase.
Formats for floating-point numbers
When printing a floating-point value, use scientific notation such as "1.23e4". (This indicates the value 1.23 times 10 to the 4th power.)
When printing a floating-point value, use fixed-point notation such as "12300.0'.
This is often used together with showpoint and setprecision.
When printing a floating-point value that has no digits to be printed to the right of the decimal point, go ahead and print the decimal point anyway.
This is usually used together with fixed and setprecision.
This switches off the effect of showpoint.
This is the default, but we might need it to undo the effect of showpoint.
This controls the number of digits to be written to the right of the decial points. Thus setprecision(2) means that we want 2 digits printed to the right of the decimal point.
The default value is typically 6.
This is usually used together with fixed and showpoint.
When printing a floating-point number using setw, use "internal justification": the first character is '+' or '-', followed possibly by fill characters, followed by the rest of the value right-justified.
Note about fixed vs. scientific: The default is neither of these. If you don't indicate your choice, C++ will choose a format for you.
Formats for bool values
When printing a bool value, spell out the word 'true' or 'false'.
When printing a bool value, print an integer 1 for 'true' or 0 for 'false'.
This is the default (which seems odd), but we might need it to undo the effect of boolalpha.
What is needed to make use of these?
The specifiers which are simply flags, such as hex, oct, dec, left, right, etc. are in the iostream library.
The specifiers which are functions, that is, which take arguments, such as setw, setprecision, setfill, etc. are in the iomanip library.
This is why we commonly have two #include statements for these libraries in our programs:
#include <iostream> #include <iomanip>
For each of these names, we wil also need a using statement, as in
using std::cout;
Alternate notation
Although the specifiers listed above are normally used with cout, it is also possible to set any of these with actual function calls, as in:
cout.setf(ios_base::oct, ios_base::basefield); cout.setf(ios_base::scientific, ios_base::floatfield); cout.precision(4); cout.width(7); cout.setf(ios_base::left, ios_base::adjustfield); cout.fill('?'); cout.flush();
Here: