Output Stream Formatting

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
File streams Chapter , ,
I/O Streams as an Introduction to Objects and Classes
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
Programming Stream Manipulators. COMP102 Prog. Fundamentals I: Stream Manipulator Slide 2 Stream Manipulators in iostream.h Library I/O stream manipulators.
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.
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction 21.2Streams Iostream Library Header.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.
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.
FORMATTED INPUT OUTPUT. Topics to be discussed……………….. Formatted Console I/O operationsFormatted Console I/O operations Defining field width :Width()Defining.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,
CS Class 13 Today  File I/O (reading from and writing to files)  Reading until EOF  Formatting output  Nested Loops Announcements  Programming.
Output Formatting. Precision #include... float grade = f; cout.precision(4); cout
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
C++ Streams Lecture-2.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
The C++ Programming Language Streams. Contents u Output Stream u Input Stream u Formatting u Manipulators u Files & Streams.
CPS120: Introduction to Computer Science Formatted I/O.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 11- C++ Stream Input/Output Outline 11.1Introduction 11.2Streams Iostream Library Header.
Student Book Input / Output in C++
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
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.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
#include int main() { char *string1=”C++”; char *string2=”Program”; int m=strlen(string1); int n=strlen(string2); for(int i=1;i
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.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
C++ ReviewEECE 3521 EECS 352 Data Structures and Algorithms José M. Vidal Office: 3A47.
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.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
I/O Streams as an Introduction to Objects and Classes
Today Variable declaration Mathematical Operators Input and Output Lab
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Chapter 21 - C++ Stream Input/Output
Math Library and IO formatting
Formatting Screen Input/Output
Chapter 6 I/O Streams as an Introduction to Objects and Classes 1.
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 5 Input and Output Streams
Managing console i/o operations
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Programming with ANSI C ++
C++ Stream Input/Output
Output Formatting Bina Ramamurthy 4/16/2019 BR.
C++ Programming Lecture 8 File Processing
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

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