Download presentation
Presentation is loading. Please wait.
Published byElla Armstrong Modified over 9 years ago
1
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting
2
Lecture outline Announcements/reminders Lab 2 (Wednesday session) due 02/08 (Friday) Lab 2 (Monday session) due on 02/13 (Wednesday) Will submit to M:\ECE-264\ Review: output formatting Changing precision Forcing decimal point to be displayed Today Continue with output formatting Changing field widths Changing justification within fields Changing fill characters File I/O Writing Code Together (WCT session) NEW!! 1/10/2016 ECE 264: Lecture 7 2
3
Review Output formatting Change base with dec/oct/hex or setbase() Change precision (# places after decimal point) with precision() or setprecision() Be sure to specify fixed format! Force decimal point to be shown with showpoint 1/10/2016 ECE 264: Lecture 7 3
4
Field Width ( width, setw ) Field width (for ostream ) Number of character positions in which value is outputted Fill characters are inserted as padding Values wider than the field are not truncated (for istream ) Maximum number of characters inputted For char array, maximum of one fewer characters than the width will be read (to accommodate null character) 1/10/2016 ECE 264: Lecture 7 4
5
Field Width ( width, setw ) (Cont.) Field width (Cont.) Member function width of base class ios_base Sets the field width Returns the previous width width function call with no arguments just returns the current setting Parameterized stream manipulator setw Sets the field width Field width settings are not sticky 1/10/2016 ECE 264: Lecture 7 5
6
Example 5: width // Fig. 15.10: Fig15_10.cpp: Demonstrating member function width. #include using std::cin; using std::cout; using std::endl; int main() { int widthValue = 4; char sentence[ 10 ]; cout << "Enter a sentence:" << endl; // set field width, then display characters based on that width do { cin.width( 5 ); // input only 5 characters from sentence cin >> sentence; cout.width( widthValue++ ); cout << sentence << endl; } while (sentence[0] != ‘.’); // end while return 0; } // end main 1/10/2016 ECE 264: Lecture 7 6
7
Example 5 output Enter a sentence: This is a test of the width member function. This is a test of the widt h memb er func tion. 1/10/2016 ECE 264: Lecture 7 7
8
Justification ( left, right and internal ) Justification in a field Manipulator left fields are left-justified padding characters to the right Manipulator right fields are right-justified padding characters to the left Manipulator internal signs or bases on the left showpos forces the plus sign to print showbase forces the base to print (for octal/hex) magnitudes on the right padding characters in the middle 1/10/2016 ECE 264: Lecture 7 8
9
Example 6: right/left justification // Fig. 15.14: Fig15_14.cpp--Demonstrating left justification and right justification. #include using std::cout; using std::endl; using std::left; using std::right; #include using std::setw; int main() { int x = 12345; // display x right justified (default) cout << "Default is right justified:" << endl << setw( 10 ) << x; // use left manipulator to display x left justified cout << "\n\nUse std::left to left justify x:\n" << left << setw( 10 ) << x; // use right manipulator to display x right justified cout << "\n\nUse std::right to right justify x:\n" << right << setw( 10 ) << x << endl; return 0; } // end main 1/10/2016 ECE 264: Lecture 7 9
10
Example 7: internal justification // Fig. 15.15: Fig15_15.cpp // Printing an integer with internal spacing and plus sign. #include using std::cout; using std::endl; using std::internal; using std::showpos; #include using std::setw; int main() { // display value with internal spacing and plus sign cout << internal << showpos << setw( 10 ) << 123 << endl; return 0; } // end main 1/10/2016 ECE 264: Lecture 7 10 + 123 Output:
11
Padding ( fill, setfill ) Padding in a field Fill characters are used to pad a field Member function fill Specifies the fill character Spaces are used if no value is specified Returns the prior fill character Stream manipulator setfill Specifies the fill character 1/10/2016 ECE 264: Lecture 7 11
12
Example 8: setfill, setw // Fig. 15.16: Fig15_16.cpp // Using member-function fill and stream-manipulator setfill to change // the padding character for fields larger than the printed value. #include using std::cout; using std::dec; using std::endl; using std::hex; using std::internal; using std::left; using std::right; using std::showbase; #include using std::setfill; using std::setw; 1/10/2016 ECE 264: Lecture 7 12
13
Example 8: setfill, setw (cont.) int main() { int x = 10000; // display x cout << x << " printed as int right and left justified\n" << "and as hex with internal justification.\n" << "Using the default pad character (space):" << endl; // display x with base cout << showbase << setw( 10 ) << x << endl; // display x with left justification cout << left << setw( 10 ) << x << endl; // display x as hex with internal justification cout << internal << setw( 10 ) << hex << x << endl << endl; // display x using padded characters (right justification) cout << right; cout.fill( '*' ); cout << setw( 10 ) << dec << x << endl; // display x using padded characters (left justification) cout << left << setw( 10 ) << setfill( '%' ) << x << endl; // display x using padded characters (internal justification) cout << internal << setw( 10 ) << setfill( '^' ) << hex << x << endl; return 0; } // end main 1/10/2016 ECE 264: Lecture 7 13
14
Example: setfill, setw (output) 1/10/2016 ECE 264: Lecture 7 14
15
WCT (Writing Code Together) Session Write a program that converts integer Fahrenheit temperatures from 0 to 212 degrees to floating-point Celsius temperatures with 3 digits of precision. Use the formula celsius = 5.0 / 9.0 * ( fahrenheit - 32 ); to perform the calculation. The output should be printed in two right-justified columns and the Celsius temperature should be preceded by a sign for both positive and negative values 1/10/2016 ECE 264: Lecture 7 15
16
WCT Session Expected output 1/10/2016 ECE 264: Lecture 7 16
17
WCT Session 1/10/2016 ECE 264: Lecture 7 17
18
Final notes Next time Introduce classes Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: Deitel & Deitel, C++ How to Program, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 1/10/2016 ECE 264: Lecture 7 18
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.