Formatting Output iomanip.h Objects Escape Sequences setw()

Slides:



Advertisements
Similar presentations
Programming Stream Manipulators. COMP102 Prog. Fundamentals I: Stream Manipulator Slide 2 Stream Manipulators in iostream.h Library I/O stream manipulators.
Advertisements

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.
Simplest program and Data Output Sen Zhang. The simplest program looks like a single person company! void main() { // this starts a comment line // here.
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.
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.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
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.
Exposure C++ Chapter VII Program Input and Output.
1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.
C++ Character Set It is set of Characters/digits/symbol which is valid in C++. Example – A-Z, (white space) C++ Character Set It is set of.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
Chapter 2 Data types, declarations, and displays.
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:
Syntax and Semantics, and the Program Development Process ROBERT REAVES.
CPS120: Introduction to Computer Science Formatted I/O.
is a specific set of data values along with a set of operations on those values. Review * Data type.
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
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.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
Library Functions. CSCE 1062 Outline  cmath class library functions {section 3.2}  iomanip class library functions {section 8.5}  string class library.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
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.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
C++ Basic Input and Output (I/O)
Topic Pre-processor cout To output a message.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Arrays Arrays exist in almost every computer language.
Chapter 3 L7.
EGR 2261 Unit 3 Input/Output Read Malik, Chapter 3.
Introduction to C++.
Output Stream Formatting
Math Library and IO formatting
Input and Output Chapter 3.
Standard Input/Output Streams
Chapter 2 – Getting Started
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
2.1 Parts of a C++ Program.
Writing Your First C++ Programs
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
Introduction to cout / cin
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
Review Data type is a specific set of data values along with a set of operations on those values. *
Formatted Input, Output & File Input, Output
CHAPTER 2: COMPONENTS OF PROGRAMMING LANGUAGE
Let’s all Repeat Together
Fundamental Programming
Output Formatting Bina Ramamurthy 4/16/2019 BR.
C++ Programming Basics
Introduction to cout / cin
Input / output (i/o) Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Programming Fundamental-1
Presentation transcript:

Formatting Output iomanip.h Objects Escape Sequences setw() setiosflags(…) setprecision()

Output ¾ cout The object cout is used to direct data to the ststandard output device, usually the monitor. w predefined stream object w in iostream library w used with insertion operator << *

Output ¾ cout Syntax: cout << ExprOrString; Read << as “put to”. cout << 24; cout << ‘y’; cout << “Hello”; *

Output ¾ cout Syntax: cout << ExprOrString; cout << “The answer to question “; cout << qu_num; cout << “ is “; cout << 1.5 * pay_rate; *

Output ¾ cout Syntax: cout << ExprOrString << ExprOrString; Each appends data to the stream. cout << “Num1 * num2 is “ << num1*num2; OUTPUT Num1 * num2 is 110 space * *

Output ¾ cout Syntax: cout << ExprOrString << ExprOrString; Each appends data to the stream. cout << “Num =“ << 35.1; OUTPUT Num =35.1 no space * *

Output ¾ cout Syntax: cout << ExprOrString << ExprOrString; Each appends data to the stream. cout << “Hi, “ << name << “how are you?”; no space space OUTPUT Hi, Shadowhow are you? * * *

Output ¾ cout Syntax: cout << ExprOrString << ExprOrString; cout << “The answer to question “; cout << qu_num; cout << “ is “; cout << 1.5 * pay_rate; no ; cout << “The answer to question “ << qu_num << “ is “<< 1.5 * pay_rate; * *

Output ¾ cout Syntax: cout << ExprOrString << ExprOrString Output ¾ cout Syntax: cout << ExprOrString << ExprOrString << ExprOrString ; cout << “The answer to question number 17 is listed after question “; cout << lastqu << ‘.’; cout << “The answer to question number 17 “ << “is listed after question “ << lastqu << ‘.’; no ; * *

Escape Sequences \ Changes the meaning of the character that follows it. \n hard return \t tab \\ \ \” “ \a beep or bell These must be within quotation marks.

Escape Sequences cout << “Name\tTotal\tGrade\n”; cout << “Miyo\t186\t B\n”; cout << “\n Jake\t211\t A\n”; cout << “Syd\t203\t A\n”; OUTPUT Name Total Grade Miyo 186 B Jake 211 A Syd 203 A _ \n \n gives a blank line *

Escape Sequences \ Changes the meaning of the character that follows it. \” take quotes literally “Alfonsus \”Butch\” Billone” gives Alfonsus ”Butch” Billone *

Escape Sequences “\n Alfonsus\n\”Butch\” \nBillone” gives Alfonsus “Butch” Billone “|\n Alfonsus\n \t \”Butch\” \tBillone|” gives | Alfonsus “Butch” Billone| * * * *

Formatting Output endl does the same thing as \n - inserts a hard return requires the insertion operator cout << endl << endl; is the same as: cout << “\n\n”;

Formatting Output iomanip.h contains the objects which have special effects on the iostream. #include <iostream.h> #include <iomanip.h> *

Formatting Output iomanip Formatting Output iomanip.h contains the objects which have special effects on the iostream.…… …... setw(n) how many columns for data setprecision(n) sets number of decimals setiosflags(ios::fixed) displays 6 digits after the decimal * * *

Formatting Output setw(n) how many columns for data cout << setw(4) << ans cout << setw(1) << ans << setw(5) << num << setw(3) << num << setw(4) << “Hi”; << setw(3) << “Hi”; fields expand ¨o33¨7132¨oHi 337132¨Hi * *

Formatting Output setprecision(n) sets number of decimals x = format result 314.0 setw(10) setprecision(2) ¨ooo314.00 point counts 314.0 setw(10) setprecision(5) ¨314.00000 columns added 314.0 setw(7) setprecision(5) 314.00000 * * *

Formatting Output setiosflags(ios::fixed) displays 6 digits after Formatting Output setiosflags(ios::fixed) displays 6 digits after the decimal #include<iostream.h> #include<iomanip.h> void main() { double v = 0.00123456789; double w = 1.23456789; double x = 12.3456789; double y = 1234.56789; double z = 12345.6789;

Formatting Output cout <<v<< endl<<w<< endl <<x<< endl <<y<< endl <<z<<”\n\n”; cout << setiosflags(ios::fixed); cout <<v<< endl <<w<< endl <<x<< endl cout << setprecision(2); }

Formatting Output default 0.00123457 1.23457 12.3457 1234.57 12345.7 ios::fixed 0.001235 1.234568 12.345679 1234.567890 12345.678900 + setprecision(2) 0.00 1.23 12.35 1234.57 12345.68

Formatting Output For decimal alignment use: ios::fixed and setprecision( ) sitcky setw( ) only once cout << setiosflags(ios::fixed) << setprecision(n); l l l cout << “- -” << setw(n) << var1 << setw(n) << “- -”; cout << “- -\t” << setw(n) << var1 << setw(n) <<var2; *

cout Syntax cout << ExprOrStringOrManipulator << ExprOrStringOrManipulator...;

Good luck is nothing but prepardness and opportunity coming together.