Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.

Slides:



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

More Review, with Some New Chapter 3. Review C++ simple data types – Integral and float – Arithmetic operators Expressions and expression evaluation –
Numeric Types, Expressions, and Output ROBERT REAVES.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.
計算機概論實習 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:
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.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,
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.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
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.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
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 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
1 CS161 Introduction to Computer Science Topic #4.
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
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.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
Chapter 05 (Part II) Control Statements: Part II.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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.
Chapter 3 Numeric Types, Expressions, and Output
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/Output Handouts: Quiz 2, Unit 3 practice sheets.
Starting Out with C++: From Control Structures through Objects
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
Chapter 3 Input output.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Formatted Input, Output & File Input, Output
Let’s all Repeat Together
Output Formatting Bina Ramamurthy 4/16/2019 BR.
C++ Programming Lecture 8 File Processing
C++ for Engineers and Scientists Second Edition
Chapter 3 The New Math.
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or showpoint, we need to include the header file iostream. #include <iostream> If we want to use setw or setprecision, we need to include the header file iomanip. #include <iomanip>

The manipulator setw Setw means set width. The argument of setw is an integer. This integer gives the field width. The output is right-justified. Example: int ans = 33; int num = 7132; Output cout << setw(4) << ans  33 7132  Hi << setw(5) << num << set(4) <, “Hi”; Cout << setw(1) << ans 33 7132 << setw(5) << num; 4 5 4 5 Field width will expand to fit the 2-digit value

The manipulator setprecision Value of x Statement Output 310.0 cout << setw(10) << setprecision(2) << x;    310.00 << setprecision(5) << x; 310.00000 310.0 cout << setw(7) << setprecision(5) << x; 310.00000 (expands width to 9) 4.827 cout << setw(6) << setprecision(2) << x;  4.83 (last displayed digit is round off)

Manipulators fixed and showpoint You can use manipulator named fixed to force all subsequent floating point output to appear in decimal form rather than scientific notation: cout << fixed << 3.8 * x; To force decimal points to be displayed in subsequent floating-point output, even for whole numbers, you can use the manipulator showpoint: cout << showpoint << floatVar;