



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























21.6 Stream Manipulators
whitespace in the input stream
1 // Fig. 21.16: fig21_16.cpp 2 // Using hex, oct, dec and setbase stream manipulators. 3 #include
10
11 using std::hex;
12 using std::dec;
13 using std::oct;
14 using std::setbase;
15
16 int main()
17 {
18 int n;
19
20 cout << "Enter a decimal number: ";
21 cin >> n;
22
23 cout << n << " in hexadecimal is: "
24 << hex << n << '\n'
25 << dec << n << " in octal is: "
26 << oct << n << '\n'
27 << setbase( 10 ) << n << " in decimal is: "
28 << n << endl;
29
30 return 0;
31 }
21.6.3 Field Width (setw,width)
output or number of characters that should be input)
as padding
cin >> setw(5) >> string;
character
1 // fig21_18.cpp
2 // Demonstrating the width member function
3 #include
4
5 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 int main()
10 {
11 int w = 4;
12 char string[ 10 ];
13
14 cout << "Enter a sentence:\n";
15 cin.width( 5 );
16
17 while ( cin >> string ) {
18 cout.width( w++ );
19 cout << string << endl;
20 cin.width( 5 );
21 }
22
23 return 0;
21.6.4 User-Defined Manipulators
21.7 Stream Format States
21.7.2 Trailing Zeros and Decimal Points
( ios::showpoint )
point and trailing zeros
cout.setf(ios::showpoint)
cout << 79;
79 will print as 79.
21.7.3 Justification ( ios::left, ios::right,
ios::internal )
1 // Fig. 21.22: fig21_22.cpp 2 // Left-justification and right-justification. 3 #include
10 using std::ios;
11 using std::setw;
12 using std::setiosflags;
13 using std::resetiosflags;
14
15 int main() 16 {
17 int x = 12345;
18
19 cout << "Default is right justified:\n"
20 << setw(10) << x << "\n\nUSING MEMBER FUNCTIONS"
21 << "\nUse setf to set ios::left:\n" << setw(10);
22
23 cout.setf( ios::left, ios::adjustfield ); 24 cout << x << "\nUse unsetf to restore default:\n";
25 cout.unsetf( ios::left );
26 cout << setw( 10 ) << x
27 << "\n\nUSING PARAMETERIZED STREAM MANIPULATORS"
28 << "\nUse setiosflags to set ios::left:\n"
29 << setw( 10 ) << setiosflags( ios::left ) << x
30 << "\nUse resetiosflags to restore default:\n"
31 << setw( 10 ) << resetiosflags( ios::left )
32 << x << endl; 33 return 0;
34 }
Default is right justified: 12345
Use setf to set ios::left: 12345 Use unsetf to restore default: 12345
Use setiosflags to set ios::left: 12345 Use resetiosflags to restore default: 12345
1 // Fig. 21.24: fig21_24.cpp
2 // Using the fill member function and the setfill
3 // manipulator to change the padding character for
4 // fields larger than the values being printed.
5 #include
6
7 using std::cout;
8 using std::endl;
9
10 #include
11
12 using std::ios;
13 using std::setw;
14 using std::hex;
15 using std::dec;
16 using std::setfill;
17
18 int main()
19 {
22 cout << x << " printed as int right and left justified\n"
23 << "and as hex with internal justification.\n"
24 << "Using the default pad character (space):\n";
25 cout.setf( ios::showbase );
26 cout << setw( 10 ) << x << '\n';
27 cout.setf( ios::left, ios::adjustfield );
28 cout << setw( 10 ) << x << '\n';
29 cout.setf( ios::internal, ios::adjustfield );
30 cout << setw( 10 ) << hex << x;
31
32 cout << "\n\nUsing various padding characters:\n";
33 cout.setf( ios::right, ios::adjustfield );
34 cout.fill( '*' );
35 cout << setw( 10 ) << dec << x << '\n';
36 cout.setf( ios::left, ios::adjustfield );
37 cout << setw( 10 ) << setfill( '%' ) << x << '\n';
38 cout.setf( ios::internal, ios::adjustfield );
39 cout << setw( 10 ) << setfill( '^' ) << hex << x << endl;
40 return 0;
41 }
10000 printed as int right and left justified and as hex with internal justification. Using the default pad character (space): 10000 10000 0x 2710
Using various padding characters:
10000%%%%%