Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: 1.0000 prints as 1 – No trailing zeros:

Slides:



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

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Numeric Types, Expressions, and Output ROBERT REAVES.
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.
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:
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
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.
FORMATTED INPUT OUTPUT. Topics to be discussed……………….. Formatted Console I/O operationsFormatted Console I/O operations Defining field width :Width()Defining.
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.
Exposure C++ Chapter VII Program Input and Output.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
Definition Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
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.
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.
CPS120: Introduction to Computer Science Formatted I/O.
Chapter 3: Input/Output
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
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.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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.
Chapter 3 Selection Statements
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.
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Chapter 3: Expressions and Interactivity.
Output Stream Formatting
Math Library and IO formatting
Formatting Screen Input/Output
Input and Output Chapter 3.
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
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
Chapter 3 Input output.
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
Formatted Input, Output & File Input, Output
Let’s all Repeat Together
Output Formatting Bina Ramamurthy 4/16/2019 BR.
C++ for Engineers and Scientists Second Edition
Lecture 3 Expressions, Type Conversion, and string
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

Output Formatting No, I don't want 6 digits…

Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros: prints as 1.5 – Use 6 digits : prints as – Scientific notation for large/small numbers: prints as e+09

Output and Formatting Output cout accepts expressions or manipulators – if expression, value is printed – if manipulator, output format is modified

cout options Manipulators : special values that tell cout how to do its job cout << "Hey – show 2 decimal places" << ; Basic manipulator : endl Others in iomanip library #include

showpoint Manipulator showpoint forces output to show the decimal point and trailing zeros Example: cout << 15.0 << " " << showpoint << 15.0 << " "; cout << 4.0 << endl; Persistent

fixed Manipulator Doubles output in floating-point format: cout << << endl; e+009 fixed forces output as fixed decimal points: cout << fixed; cout << << endl; Persistent

fixed Manipulator scientific manipulator – Return to floating point format cout << fixed; cout << << endl; cout << scientific; cout << << endl; e+009 Persistent

setprecision Manipulator setprecision(n) – Number of decimal places before value is rounded cout << fixed << setprecision(2); cout << << " "; cout << << endl; Persistent

setw setw(x) – Makes next expression take up at least x columns Adds spaces to left cout << 5 << setw(5) << 12 << endl; – Longer expressions not affected: cout << 5 << setw(2) << 12123;

setw setw(x) – Applies only to next expression cout << setw(6) << 12 << 12; cout << setw(6) << 12 << setw(6) << 12; NOT Persistent

Additional Output Formatting Tools setfill(char) – What char to use instead of spaces to fill columns left and right manipulators – Which side to justify text cout << left << setfill('*'); cout << setw(5) << 12 << 10; 12***10 Persistent

Goal Read in 2 numbers – numerator, denominator Print something like this: Enter numbers: 2 3 Numerator/Denominator Decimal Value 2/3 0.67

Goal Visualize spaces: Enter numbers: 2 3 Numerator/Denominator..Decimal Value / Break into pieces: : right, width of : left, width of : 2 decimal places, fixed

Finished Program