1 / 15

ECE 264 Object-Oriented Software Development

ECE 264 Object-Oriented Software Development. Instructor: Dr. Honggang Wang Fall 2012 Lecture 7: Continuing with output formatting; File I/O. Lecture outline. Announcements/reminders Lab 2 due Mon. 09/24 Will submit to M:ECE-264<username> Review: output formatting Changing precision

tea
Download Presentation

ECE 264 Object-Oriented Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 264Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 7: Continuing with output formatting; File I/O

  2. Lecture outline • Announcements/reminders • Lab 2 due Mon. 09/24 • Will submit to M:\ECE-264\<username> • 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 ECE 264: Lecture 7

  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 ECE 264: Lecture 7

  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) ECE 264: Lecture 7

  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 ECE 264: Lecture 7

  6. Example 5: width // Fig. 15.10: Fig15_10.cpp: Demonstrating member function width. #include <iostream> 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 ECE 264: Lecture 7

  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 . ECE 264: Lecture 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 ECE 264: Lecture 7

  9. Example 6: right/left justification // Fig. 15.14: Fig15_14.cpp--Demonstrating left justification and right justification. #include <iostream> using std::cout; using std::endl; using std::left; using std::right; #include <iomanip> 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 ECE 264: Lecture 7

  10. Example 7: internal justification // Fig. 15.15: Fig15_15.cpp // Printing an integer with internal spacing and plus sign. #include <iostream> using std::cout; using std::endl; using std::internal; using std::showpos; #include <iomanip> using std::setw; int main() { // display value with internal spacing and plus sign cout << internal << showpos << setw( 10 ) << 123 << endl; return 0; }// end main Output: + 123 ECE 264: Lecture 7

  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 ECE 264: Lecture 7

  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 <iostream> 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 <iomanip> using std::setfill; using std::setw; ECE 264: Lecture 7

  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 ECE 264: Lecture 7

  14. Example: setfill, setw (output) ECE 264: Lecture 7

  15. Final notes • Next time • File I/O • Introduce classes • Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: • Deitel & Deitel, C++ How to Program, 8th ed. • Etter & Ingber, Engineering Problem Solving with C++, 2nd ed. ECE 264: Lecture 7

More Related