Manipulators CSCE 121 J. Michael Moore

Slides:



Advertisements
Similar presentations
Chapter 3 Objects, types, and values Bjarne Stroustrup
Advertisements

Chapter 11 Customizing I/O Bjarne Stroustrup
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 11 Customizing I/O Hartmut Kaiser
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
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.
計算機概論實習 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall A. Using Class Type Objects.
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
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.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
Chapter 11 Customizing I/O Bjarne Stroustrup
CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
Definition Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators.
C++ Streams Lecture-2.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
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.
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.
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.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
C++ Basic Input and Output (I/O)
Chapter 11 Customizing I/O
Standard Input - Output
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
Output Stream Formatting
Chapter 11 Customizing I/O
Binary, Octal and Hex Numbers Copyright Thaddeus Konar
Binary, Octal and Hex Numbers Copyright Thaddeus Konar
Binary Quiz UIN: ____________________
Anatomy of a Function Part 2
Binary, Octal and Hex Numbers Copyright Thaddeus Konar
IO Overview 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.
Functions Overview CSCE 121 J. Michael Moore
Anatomy of a Function Part 3
Input Validation CSCE 121 J. Michael Moore
There are 10 types of people of people in this world…
Introduction to cout / cin
Section 4.3 Scientific Notation.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 3: Expressions and Interactivity
Chapter 11 Customizing I/O
Guidelines for Writing Functions
Chapter 11 Customizing I/O
Remember the 10 types of people of people in this world…
Introduction to cout / cin
IO Overview CSCE 121 Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch.
Presentation transcript:

Manipulators CSCE 121 J. Michael Moore Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch

Manipulators Modify the state of a stream Most manipulators are “sticky.” They are set and are permanent until changed again. http://www.cplusplus.com/reference/library/manipulators/

Integer Formatting Recall Decimal (base 10: digits 0-9) Octal (base 8: digits 0-7) Hexadecimal (base 16: digits 0-9, a-f) Indicate which base an input or output stream should use: dec, oct, or hex sticky

Integer Formatting Examples int x = 1234; cout << hex; // from now on, ints will be written to screen in hex: cout << x; cin >> oct; // from now on, ints will be read in from keyboard as // if they are in octal int y; cin >> y; cout << dec; // back to decimal output cin >> dec; // back to decimal input

Floating Point Formatting scientific: one digit before decimal point, n digits after decimal point, plus exponent Ex: 1.2345678e+03 use manipulator scientific fixed: n digits after decimal point, no exponent Ex: 1234.567890 use manipulator fixed n is the “precision” (coming up next)

Precision If not set to scientific or fixed, precision is the number of digits For scientific, precision is number of digits after decimal point For fixed, precision is number of digits after decimal point Use setprecision manipulator (for output streams) Ex: cout << scientific << setprecision(8);

All this is the kind of stuff you look up when you need it Width You can control the width (number of characters to be used for the output) not sticky output is never truncated to fit All this is the kind of stuff you look up when you need it