CPS120: Introduction to Computer Science Formatted I/O.

Slides:



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

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Numeric Types, Expressions, and Output ROBERT REAVES.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
計算機概論實習 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Computer Science 1620 Formatting. Suppose you work for the HR dept. of a company you wish to write a program to show their earnings per month Details:
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.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
CHAPTER 7 DATA HANDALING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,
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.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
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:
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
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:
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
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 CS161 Introduction to Computer Science Topic #4.
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
C++ REVIEW INPUT/OUTPUT (I/O). BRIEF NOTE “CLASSES” AND “STRUCTS” AND “TYPES” They are ADTs… They define data and operations on that data The ADTs in.
#include int main() { char *string1=”C++”; char *string2=”Program”; int m=strlen(string1); int n=strlen(string2); for(int i=1;i
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
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 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Chapter 05 (Part II) Control Statements: Part II.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
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.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
C++ Basics Lecture 2.
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.
TMF1414 Introduction to Programming
Output Stream Formatting
Chapter 2 part #3 C++ Input / Output
Formatting Screen Input/Output
DATA HANDLING.
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
Chapter 2 part #3 C++ Input / Output
Presentation transcript:

CPS120: Introduction to Computer Science Formatted I/O

Formatted Output -- Currency cout.setf(ios : : fixed)  Print “fixed point” form, not in exponential form cout.setf(ios : : showpoint)  Says to always print the decimal point cout.precison(2)  Says to print out the two most significant decimal digits, after rounding to this precision

Line Spacing  In order to end a line in an output statement you may use the new line character, \n, instead of endl.  Examples: cout << "Hello world" << '\n'; cout << "Hello world" << "\n"; cout << "Hello world\n";  These are practically equivalent to: cout << "Hello world" << endl;

Escape Sequences  Other useful "escape sequences" (since the \ is the escape operator) are: \t to generate a tab \\ to print a backslash \' to print a single quote \" to print a double quote

Using setf and unsetf  Each stream has format options that can be changed OPTIONDESCRIPTION leftLeft-justifies the output rightRight-justifies the output showpointDisplays decimal point and trailing zeros for floats uppercase Displays e in scientific as E showposDisplays a leading plus sign scientificDisplays floating point number scientifically fixedDisplays floating-point in normal notation

Using Format Options  Format options are set immediately prior to the COUT statement float x = 24.0; cout << x << ‘\n’;// displays 24 cout.setf(ios::showpoint); cout << x << ‘\n’;// displays cout.unsetf(ios::showpoint); cout << x << ‘\n’; // displays 24

Using Manipulators  You must include the header file at the top of your program in order to use the setprecision, setw, and other manipulators. You must use place the following compiler directive at the top of your program. #include  I/O manipulators are placed directly in the output statement cout << setprecision(2) << price << ‘\n’;

Setting Precision  The setprecision manipulator allows you to limit the number of digits that are displayed when a numeric data type is displayed: cout << setprecision(2) << price << '\n'; only allows the leading two digits of the value stored in the variable, price, to be displayed

More Precisely  If the fixed format was set previously with the statement: cout.setf(ios::fixed); then the setprecision(2) manipulator would have the effect of rounding or truncating price (and all future floating-point values in the cout stream) to the hundredths place

Field Width  The setw manipulator controls the width of the field when displaying a value. The statement: cout << setw(10) << umEndow << endl; sets the width of the field allocated for the variable, umEndow, to 10 characters

Formatting Numbers for Output double price; price = 78.5; cout << "The price is $"; cout << price << endl;  We want the price to be $78.50

Magic Formula for Currency cout.setf(ios :: fixed); cout.setf(ios :: showpoint); cout.precision(2);

Formatted Output -- Currency cout.setf(ios : : fixed)  Print “fixed point” form, not in exponential form cout.setf(ios : : showpoint)  Says to always print the decimal point cout.precison(2)  Says to print out the two most significant decimal digits, after rounding to this precision