Chapter 3 L7.

Slides:



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

CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
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:
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.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Input/Output
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.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
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,
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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.
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
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.
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.
Chapter 05 (Part II) Control Statements: Part II.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
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.
Lecture 3 Expressions, Type Conversion, Math and String
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. Expressions and Interactivity
Chapter 3: Expressions and Interactivity.
Output Stream Formatting
Math Library and IO formatting
Formatting Screen Input/Output
Input and Output Chapter 3.
Expressions and Interactivity
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Starting Out with C++: From Control Structures through Objects
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
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++ for Engineers and Scientists Second Edition
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 3 Expressions, Type Conversion, and string
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

Chapter 3 L7

Formatting output L7 Manipulator (flags) setprecision showpoints setleft setright setw stream

3.8 Formatting Output Can control how output displays for numeric, string data: size position number of digits Requires iomanip header file

Precision and formatted output How many values appear after and before the decimal point ?? cout<< setiosflags(ios::fixed | ios :: showpoint)<< setprecision (2); Or cout<< fixed << showpoint<< setprecision (2); Defined in the <iomanip.h> library

#include <iostream> #include <iomanip> Using namespase std; int main() { float x; cout << "x = "; cin>> x; cout << setiosflags (ios :: fixed | ios :: showpoint | ios:: left)<<setprecision(2); // or // cout << fixed << showpoint<< left <<setprecision(2); cout << x ; Return 0; } Input 1234.4567 Outout 1234.46

Stream Manipulators Used to control features of an output field Some affect just the next value displayed: setw(x): print in a field at least x spaces wide. Use more spaces if field is not wide enough Some affect values until changed again: fixed: use decimal notation for floating-point values setprecision(x): when used with fixed, prints floating-point value using x digits after the decimal. Without fixed, prints floating-point value using x significant digits // show an example of both Mix18.cpp showpoint: always print decimal for floating-point values )

Manipulator Examples const double e = 2.718; cout << setw(8) << e; // 2.718 in a // field 8 wide cout << setprecision(2); cout << e; // 2.7 cout << fixed<<setprecision(2); cout << e; // 2.72 double price = 25.0, discount = 0.6; cout << fixed << setprecision(2); cout << price * discount; // 15.00

Formatting output stream manipulator setw() int Numb1 = 100, Numb2 = 200, Numb3 = 300; cout << Numb1 <<Numb2 <<Numb3; Gives us 100200300 BUT: cout << setw(5 )<<Numb1<<setw(7) << Numb2<<setw(5)<<Numb3 SS100SSSS200SS300 100 200 300

The left and right Manipulators “right is the default” float Miles = 134.567; cout << setw(12)<<Miles; SSSSS134.567 cout << setiosflags (ios::left) <<setw(12) << Miles; cout<<left<<setw(12)<<Miles; 134.567SSSSS cout << setiosflags(ios::right) << setw(12) << Miles;

Formatting output setprecision manipulator setprecision() float Num1 = 2100.123456; cout << Num1; 2100.123456 cout << fixed<<setprecision(4)<<Num1; 2100.1235 cout << fixed<<setprecision(5)<<Num1; 2100.12346 cout <<fixed<<setprecision(6)<<Num1;

BIG difference between set precision in Turbo and .net compilers Look at table 3-11 page 109 Compare with the outcome of this program. #include<iostream.h> #include<iomanip.h> int main() { float Num1 = 28.92786; cout<< setiosflags(ios::fixed)<< setprecision (3) << Num1; return 0; } Output 28.928 on turbo and DEV, 28.9 on dot net !!

Formatting output setprecision manipulator setprecision() float Num1 = 2100.123456; cout << setprecision(2)<<Num1; 2.1e+03 cout << setprecision(12)<<Num1; 2100.12345688 !!!!

Formatting output manipulator setprecision + fixed() Forces cout to print the digits in fixed point notation or decimal. float Num1 = 210.123456; cout<< setiosflags(ios::fixed)<< setprecision (2) << Num1; 210.12 cout<< setiosflags(ios::fixed)<< setprecision (4) << Num1; 210.1234

Formatting output manipulator showpoint() Forces cout to display the trailing zeros. float Num1 = 210.0000 ; cout << Num1; 210 cout<< setiosflags(ios :: showpoint)<< Num1; 210.0000 NOTE: Cout <<setprecision(2)<<showpoint; // works for .net AND FOR DEV but not for Turbo

showpoint and setprecision float Num1 = 210.0000 ; cout<< setprecision(6)<< Num1; 210 // would not show the 0000 cout setsettiosflags (ios::showpoint) <<setprecision(6)<< Num1; 210.000000

showpoint and setprecision causes the decimal points and trailing zeros to be displayed EVEN if there is no fractional part float Num1 = 210 ; cout<< setiosflags(ios :: showpoint) << setprecision(6) << Num1; 210.000000

The fill( ) function cout.fill (‘x’) Changes the fill character, whicjh is by default a space to be something else, ie x. float Miles = 134.567; cout << setiosflags(ios :: right) << setw(12) << Miles; SSSSS134.567 cout.fill('x'); cout << setiosflags(ios :: right) << setw(12) <<Miles; xxxxx134.567

setw() and strings cout<<“I Love C++ Programming”; Will give you: I Love C++ Programming cout<<setw(22)<<“I Love C++ Programming”; Would give you the same, as long as its 22, or less. cout<<setw(30)<<“I Love C++ Programming”; SSSSSSSSI Love C++ Programming // right justified

When the size of the setw() less or = the size of the string // This program demonstrates the setw manipulator being // used with values of various data types. #include <iostream> #include <iomanip> using namespace std; int main() { int intValue = 3928; double doubleValue = 91.5; const int ARRAY_SIZE = 14; char cStringValue[ARRAY_SIZE] = "John J. Smith"; cout << "(" << setw(5) << intValue << ")" << endl; cout << "(" << setw(8) << doubleValue << ")" << endl; cout << "(" << setw(16) << cStringValue << ")" << endl; // cout << "(" << setw(16) << cStringValue << ")" << endl system("pause"); return 0; }

Program example// notice the changes from yours // we will run this program to demonstrate how flags work // if statments and loops wil be covered soon #include <iostream> #include <iomanip> using namespace std; int main() { float weight; int i; for (i = 0; i < 6; i++) cout<< fixed << showpoint<< setprecision (2); //cout<< setiosflags(ios :: fixed |ios :: showpoint)<< setprecision (2); cout << " \n enter weight \n" ; cin >> weight; if (weight > 150 ) cout << weight << setw(3)<< " over weight "; else if (weight > 100 && weight < 150) cout<< weight<<setw(3) << " no risk "; else cout << weight<<setw(3)<<" too thin "; } return 0;