Download presentation
Presentation is loading. Please wait.
Published byRichard Perkins Modified over 9 years ago
1
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting
2
Lecture outline Announcements/reminders Lab 2 due Mon. 09/24 Will submit to M:\ECE-264\ TA office: SENG-211. 10-11 am (Tue), 11-12:00 pm (Thur) Attendance ( bonus quiz in class: 3%, required for lab) Review get(), getline(), and ignore() Started output formatting—bases Continue with output formatting 10/1/2015 ECE 264: Lecture 6 2
3
Review Input ( cin ) streams Use cin to read values into variables E.g., cin >> x; Skips whitespace characters Input value must be compatible with type of x Reading n characters: cin.get(buffer, n); Reading 1 character: cin.get(ch); Reading an entire line (at most m characters): cin.getline(buffer, m) May need cin.ignore(x) to skip characters Output formatting Change base with dec/oct/hex or setbase() 10/1/2015 ECE 264: Lecture 6 3
4
Example 2: bases //Example: Determine the output #include using std::cout; using std::endl; using std::hex; using std::oct; int main() { int i; for (i = 0; i <= 32; i += 8) { cout << "Decimal: " << i << endl; cout << oct << "Octal: " << i << endl; if ((i % 16) == 0) cout << hex << "Hexadecimal: " << i << endl; } return 0; } 10/1/2015 ECE 264: Lecture 6 4
5
Example 2: bases (cont.) Output: Decimal: 0 Octal: 0 Hexadecimal: 0 Decimal: 8 Octal: 10 Decimal: 20 Octal: 20 Hexadecimal: 10 Decimal: 18 Octal: 30 Decimal: 40 Octal: 40 Hexadecimal: 20 10/1/2015 ECE 264: Lecture 6 5 What’s the problem? Base settings are sticky How would we fix it? Insert “ dec ” into the cout statement for the decimal values
6
FP Precision ( precision, setprecision ) Precision of floating-point numbers Number of digits displayed to the right of the decimal point setprecision parameterized stream manipulator precision member function When called with no arguments, returns the current precision setting Should always use with fixed format Precision settings are sticky 10/1/2015 ECE 264: Lecture 6 6
7
Example 3: setprecision // Fig. 15.9: Fig15_09.cpp // Controlling precision of floating-point values. #include using std::cout; using std::endl; using std::fixed; #include using std::setprecision; #include using std::sqrt; // sqrt prototype 10/1/2015 ECE 264: Lecture 6 7
8
Example 3: setprecision (cont.) int main() { double root2 = sqrt( 2.0 ); // calculate square root of 2 int places; // precision, vary from 0-9 cout << "Square root of 2 with precisions 0-9." << endl << "Precision set by ios_base member function " << "precision:" << endl; cout << fixed; // use fixed point format // display square root using ios_base function precision for ( places = 0; places <= 9; places++ ) { cout.precision( places ); cout << root2 << endl; } // end for cout << "\nPrecision set by stream manipulator " << "setprecision:" << endl; // set precision for each digit, then display square root for ( places = 0; places <= 9; places++ ) cout << setprecision( places ) << root2 << endl; return 0; } // end main 10/1/2015 ECE 264: Lecture 6 8
9
Example 3 output 10/1/2015 ECE 264: Lecture 6 9
10
Trailing Zeros and Decimal Points ( showpoint ) Stream manipulator showpoint Floating-point numbers are output with decimal point and trailing zeros Example 79.0 prints as 79.000000 instead of 79 Reset showpoint setting with noshowpoint Implies a default precision of 6 Can override with precision / setprecision 10/1/2015 ECE 264: Lecture 6 10
11
Example 4: showpoint, setprecision #include using std::cin; using std::cout; using std::endl; using std::fixed; using std::showpoint; #include using std::setprecision; int main() { double i, j, x, y; cin >> i >> j >> x >> y; cout << fixed << showpoint; cout << "First output " << endl; cout << i << ',' << j << ',' << setprecision(3) << x << ',' << y << endl; return 0; } // Input stream is: 1 2 3.4 5 10/1/2015 ECE 264: Lecture 6 11 First output 1.000000,2.000000,3.400,5.000 _
12
Final notes Next time Finish formatted output—field widths, justification, and fill characters 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. 10/1/2015 ECE 264: Lecture 6 12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.