Output Stream Formatting Andy Wang Object Oriented Programming in C++ COP 3330
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
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)
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
Stream member functions unsetf() // unset flags precision() // specifies the number of digits after the decimal point
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 1234
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: // ......1234
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);
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);
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
Example: Formatting Flags and Member Functions http://www.cs.fsu.edu/~myers/c++/examples/stre amio/format/formats1.cpp
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; }
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);
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”; }
Example: Stream Manipulators http://www.cs.fsu.edu/~myers/c++/examples/stre amio/format/formats2.cpp
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; }
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);
format2.cpp cout << scientific << right << setprecision(1); PrintData(a, b); cout << showpoint << setfill(‘.’); cout << internal; cout << “\nSee ya!\n”; }
Example: Integer Output in Decimal, Octal, and Hex http://www.cs.fsu.edu/~myers/c++/examples/stre amio/format/bases.cpp
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;
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; }
bases.cpp int main() { int x = 123456, y = 987543, z = 12345678; PrintValues(x, y, z); cout.setf(ios::showbase); cout.setf(ios::uppercase); return 0; }