Programming Workshop 2 (CSCI 1061U)
Faculty of Science, Ontario Tech University
C++ provides a number of facilities to control how output sent to console or output file streams is formatted. We can use these to modify the behavior of cout
.
You’ll need to include iomanip
header file to make use of these.
setw
setw
sets the minimum field width for the value to be printed. setw
only effects the next value to be printed.
setw.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{"no setw" << endl;
cout << '*' << 2 << '*'<< endl;
cout <<
'*' << "setw 10" << endl;
cout << '*' << setw(10) << 2 << '*'<< endl;
cout <<
'*' << "setw 20" << endl;
cout << '*' << setw(20) << 2 << '*'<< endl;
cout <<
'*' << "setw 0" << endl;
cout << '*' << setw(0) << 2 << '*'<< endl;
cout <<
return 0;
}
Output
no setw
*2*
*setw 10
* 2*
*setw 20
* 2*
*setw 0 *2*
left
, right
and internal
justify.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{"1" << endl;
cout << "*" << -23 << "*" << endl;
cout << "*" << setw(6) << -23 << "*" << endl;
cout <<
"2" << endl;
cout <<
cout << left;"*" << setw(6) << -23 << "*" << endl;
cout <<
"3" << endl;
cout <<
cout << internal;"*" << setw(6) << -23 << "*" << endl;
cout <<
"4" << endl;
cout <<
cout << right;"*" << setw(6) << -23 << "*" << endl;
cout <<
"5" << endl;
cout <<
cout << internal;"*" << setw(6) << -23 << "*" << endl;
cout <<
return 0;
}
Output
1
*-23*
* -23*
2
*-23 *
3
*- 23*
4
* -23*
5 *- 23*
boolalpha
and noboolapha
By default Boolean values are printed as either 0
or 1
. We can, however, choose to instead print them as false
or true
.
boolalpha.cpp
// noboolalpha and boolalpha
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{bool a = true, b = false;
"a " << a << endl;
cout << "a " << a << endl;
cout << boolalpha <<
"b " << b << endl;
cout << noboolalpha << "b " << b << endl;
cout << boolalpha <<
return 0;
}
Output
a 1
a true
b 0 b false
Octal and hexadecimal notations are often used to represent numbers in computers.
We all know decimal notation. It comprises of 10 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
A single digit decimal number can represent up to 10 values. An \(n\) digit binary number can represent up to \(10^n\) values.
Uses two symbols: 0 and 1.
A single digit binary number can represent up to 2 values. An \(n\) digit binary number can represent up to \(2^n\) values.
Octal notation comprises of 8 symbols: 0, 1, 2, 3, 4, 5, 6, 7. Any symbol in octal notation can be expressed using 3 bits as seen below.
Octal | Binary |
---|---|
0 | 000 |
1 | 001 |
2 | 010 |
3 | 011 |
4 | 100 |
5 | 101 |
6 | 110 |
7 | 111 |
A single digit octal number can represent up to 8 values. An \(n\) digit octal number can represent up to \(8^n\) values.
Octal notation comprises of 16 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. Any symbol in hexadecimal notation can be expressed using 4 bits as seen below.
Hexadecimal | Binary |
---|---|
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
A | 1010 |
B | 1011 |
C | 1100 |
D | 1101 |
E | 1110 |
F | 1111 |
A single digit hexadecimal number can represent up to 16 values. An \(n\) digit hexadecimal number can represent up to \(16^n\) values.
Hexadecimal notation is often used to display bit patterns stored in a byte. Say letter A
is stored in a byte. We know that ASCII code for letter A
is 65
. The binary value is 01000001
. Binary values are difficult to parse. It is often easier to represent it using hexadecimal notation. Note that each hexadecimal symbol takes 4 bits, so to represent 8 bits, we will use two hexadecimal symbols. Specifically, 01000001
can be conveniently written as 41
in hexadecimal notation.
You can use dec
, oct
and hex
modifiers to display numbers in decimal, octal or hexadecimal notation. Furthermore, you can use showbase
modifier to also indicate if the number is using decimal, octal or hexadecimal notation.
Consider a number 65
. Unless someone tell me, I wouldn’t know if it is using decimal, octal or hexadecimal notation. In C++, we use the following convention to indicate whether a number is decimal, octal or hexadecimal.
bases.cpp
// showbase and noshowbase --- identifies decimal, octal and hexadecimal numbers.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{int a = 234532;
"a (decimal) " << dec << a << endl;
cout << "a (octal) " << oct << a << endl;
cout << "a (hex) " << hex << a << endl;
cout <<
"With showbase" << endl;
cout <<
cout << showbase;"a (decimal) " << dec << a << endl;
cout << "a (octal) " << oct << a << endl;
cout << "a (hex) " << hex << a << endl;
cout <<
"With noshowbase" << endl;
cout <<
cout << noshowbase;"a (decimal) " << dec << a << endl;
cout << "a (octal) " << oct << a << endl;
cout << "a (hex) " << hex << a << endl;
cout <<
return 0;
}
Output
a (decimal) 234532
a (octal) 712044
a (hex) 39424
With showbase
a (decimal) 234532
a (octal) 0712044
a (hex) 0x39424
With noshowbase
a (decimal) 234532
a (octal) 712044 a (hex) 39424
uppercase
and nouppercase
These modifiers can be used to modify how hexadecimal numbers are displayed. These also effect numbers displayed using scientific notation.
case.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{int a = 234532339;
float b = 32000004032400.34;
cout << showbase << hex;"a (hex) " << a << endl;
cout <<
cout << uppercase;"a (hex) " << a << endl;
cout <<
cout << nouppercase << dec;"b (hex) " << b << endl;
cout <<
cout << uppercase;"b (hex) " << b << endl;
cout <<
return 0;
}
Output
a (hex) 0xdfaadf3
a (hex) 0XDFAADF3
b (hex) 3.2e+13 b (hex) 3.2E+13
showpos
and noshowpos
Use showpos
and noshowpos
to print +
next to a positive number.
showpos.cpp
// noshowpos and showpos --- prints a + sign next to a positive number
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{int a = 5, b = -6;
float c = 3.4, d = -35.6;
"a " << a << endl;
cout << "b " << b << endl;
cout << "c " << c << endl;
cout << "d " << d << endl;
cout <<
cout << showpos;"a " << a << endl;
cout << "b " << b << endl;
cout << "c " << c << endl;
cout << "d " << d << endl;
cout <<
return 0;
}
Output
a 5
b -6
c 3.4
d -35.6
a +5
b -6
c +3.4 d -35.6
Floats can be displayed using general, fixed
or scientific
format. The default is general, which automatically picks fixed
or scientific
based upon what yields a more “pleasant” result. For example, for 5.2
, it is better to use the fixed format; however, 32000004032400.34
looks better in scientific format.
Once you pick fixed or scientific format, there is no easy way to rever to general format. You will have to use
cout.unsetf(ios::fixed | ios::scientific);
to revert to general format.
setprecision
is used to specify roughly the number of digits displayed for the number. In general format, the setprecision specifies the maximum number of digits displayed. This includes digits before and after the decimal point, excluding the decimal point. In fixed and scientific formats, the precision specifies the number of digits after the decimal point.
showpoint
modifier can be used to display the trailing zeros.
floats.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{float a = 5.2;
float b = 32000004032400.34;
"Different formats" << endl;
cout << "General" << endl;
cout << " " << b << endl;
cout << a << "Fixed" << endl;
cout << " " << b << endl;
cout << fixed << a << "Scientific" << endl;
cout << " " << b << endl;
cout << scientific << a <<
"Back to general" << endl;
cout <<
cout.unsetf(ios::fixed | ios::scientific);" " << b << endl;
cout << a <<
"Set precision to 1" << endl;
cout << 1) << a << " " << b << endl;
cout << setprecision(
"Set precision to 20" << endl;
cout << 20) << a << " " << b << endl;
cout << setprecision(
"showpoint" << endl;
cout << 6) << showpoint << a << endl;
cout << setprecision(
return 0;
}
Output
Different formats
General
5.2 3.2e+13
Fixed
5.200000 32000004063232.000000
Scientific
5.200000e+00 3.200000e+13
Back to general
5.2 3.2e+13
Set precision to 1
5 3e+13
Set precision to 20
5.1999998092651367188 32000004063232
showpoint 5.20000
setfill
The default fill character is a space ’ ’. This is the character that is appended (or prepended) to the value when setw
is used to set the width. We can change this character using setfill
command.
fill.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{"default fill: " << setw(10) << 42 << endl;
cout << "setfill('*'): " << setfill('*');
cout << 10) << 42 << endl;
cout << setw( }
Output
default fill: 42 setfill('*'): ********42