Download presentation
Presentation is loading. Please wait.
1
Output Stream Formatting
Andy Wang Object Oriented Programming in C++ COP 3330
2
Brief Introduction cout is the standard output stream
It is of type ostream cin is the standard input stream It is of type istream ostream and istream are classes
3
Stream member functions
setf() // the set flags function Takes a parameter to turn the flag on ios::fixed // print floating numbers in fixed notation // digits before and after the ‘.’ ios::scientific // print floating numbers in scientific (exponential) notation ios::showpoint // decimal point will always be printed for floating point types (e.g., 4.0)
4
Stream member functions
setf() // the set flags function ios::right // right justifies an output item in a field // if field width is specified ios::left // left justifies an output item in a field ios::showbase // show a leading 0 for octal, 0x for hex
5
Stream member functions
unsetf() // unset flags precision() // specifies the number of digits after the decimal point
6
Stream member functions
width() // specify the field width for the next item // extra space will be filled with the fill character int x = 1234; cout.setf(io::right); cout.width(10); cout << “Hello”; cout.width(15); cout << x; // output of the above is: // Hello
7
Stream member functions
fill() // specify the fill character to pad out the extra space in a field when used with width int x = 1234; cout.setf(io::right); cout.fill(‘.’); cout.width(10); cout << x; // output of the above is: //
8
Stream Manipulators A stream manipulator is a symbol or function that is used by placing it on the right side of the insertion operator <<. Need to include <iomanip> A plain manipulator is just a symbol cout << endl; // endl is a stream manipulator A parameterized stream manipulator looks like a function call cout << setw(10);
9
Stream Manipulators Many of the stream manipulators are alternative ways of doing tasks performed by member function Good for cascading cout << setw(10) << “Hello” << endl; setprecision() ~= precision() cout.precision(2); cout << setprecision(2);
10
Stream Manipulators setw() ~= width() setfill() ~= fill()
cout.width(10); cout << setw(10); setfill() ~= fill() cout.fill(‘*’); cout. << setfill(‘*’); setiosflags() ~= setf() cout.setf(ios::left); cout << setiosflags(ios::left); setbase() // set to base 10, 8, or 16
11
Example: Formatting Flags and Member Functions
amio/format/formats1.cpp
12
format1.cpp #include <iostream> using namespace std; void PrintData(int x, double y) { cout.width(15); cout << x; cout.width(25); cout << y; cout.width(30); cout << “Hello, World” << endl; }
13
format1.cpp int main() { int a; double b; cout << “Enter an integer: “; cin >> a; cout << “Enter a double: “; cin >> b; cout << endl; PrintData(a, b); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.setf(ios::left); cout.precision(5);
14
format1.cpp cout.setf(ios::scientific); cout.setf(ios::right); cout.precision(1); PrintData(a, b); cout.setf(ios::showpo); cout.fill(‘.’); Print(a, b); cout << “\nSee ya!\n”; }
15
Example: Stream Manipulators
amio/format/formats2.cpp
16
format2.cpp #include <iostream> #include <iomanip> using namespace std; void PrintData(int x, double y) { cout << setw(15) << x; cout << setw(25) << y; cout << setw(30) << “Hello, World” << endl; }
17
format2.cpp int main() { int a; double b; cout << “Enter an integer: “; cin >> a; cout << “Enter a double: “; cin >> b; cout << endl; PrintData(a, b); cout << fixed << showpoint << left << setprecision(5);
18
format2.cpp cout << scientific << right << setprecision(1); PrintData(a, b); cout << showpoint << setfill(‘.’); cout << internal; cout << “\nSee ya!\n”; }
19
Example: Integer Output in Decimal, Octal, and Hex
amio/format/bases.cpp
20
bases.cpp #include <iostream> #include <iomanip> using namespace std; void PrintValues(int x, int y, int x) { cout int WID = 20; … cout << dec << “Decimal “ << setw(WID) << x << setw(WID) << y << setw(WID) << z << endl;
21
bases.cpp cout << oct << “Decimal “ << setw(WID) << x << setw(WID) << y << setw(WID) << z << endl; cout << hex<< “Decimal “ << setw(WID) << x << setw(WID) << y << setw(WID) << z << endl; }
22
bases.cpp int main() { int x = , y = , z = ; PrintValues(x, y, z); cout.setf(ios::showbase); cout.setf(ios::uppercase); return 0; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.