ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.

Slides:



Advertisements
Similar presentations
Programming Stream Manipulators. COMP102 Prog. Fundamentals I: Stream Manipulator Slide 2 Stream Manipulators in iostream.h Library I/O stream manipulators.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Stream Input/Output.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
計算機概論實習 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Chapter 3: Input/Output
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction 21.2Streams Iostream Library Header.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
CSCE 121: Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 - C++ Stream Input/Output Outline 12.1 Introduction 12.2 Streams Classic Streams vs.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
ECE 264 Object-Oriented Software Development Instructor: Dr. Michael Geiger Spring 2009 Lecture 2: Basic C++ Programs.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Definition Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 2: Software Design Cycle.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
CPS120: Introduction to Computer Science Formatted I/O.
1 Lecture 3 Control Structures else/if and while.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 11- C++ Stream Input/Output Outline 11.1Introduction 11.2Streams Iostream Library Header.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Chapter 05 (Part II) Control Statements: Part II.
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 2: Software Design Cycle, Requirements Specification and.
Topic 2 Input/Output.
ECE 264 Object-Oriented Software Development
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Output Stream Formatting
Chapter 2 part #3 C++ Input / Output
Standard Input/Output Streams
Manipulators CSCE 121 J. Michael Moore
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Chapter 12 - C++ Stream Input/Output
C++ Stream Input/Output
Chapter 2 part #3 C++ Input / Output
Chapter 12 - C++ Stream Input/Output
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings
Presentation transcript:

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting

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

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

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

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

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

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

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

Example 3 output 10/1/2015 ECE 264: Lecture 6 9

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 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

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/2015 ECE 264: Lecture 6 11 First output , ,3.400,5.000 _

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