SlideShare a Scribd company logo
1 of 17
FORMATTED INPUT AND OUTPUT
Objectives
In this chapter, you will:
• Learn what a stream is and examine input and
   output streams
• Explore how to read data from the standard input
   device
• Learn how to use predefined functions in a program
• Learn how to write data to the standard output
   device
• Discover how to use manipulators in a program to
   format output


                                                       2
I/O Streams and Standard I/O Devices
• I/O: sequence of bytes (stream of bytes) from source
  to destination
   – Bytes are usually characters, unless program requires
     other types of information
• Stream: sequence of characters from source to
  destination
• Input stream: sequence of characters from an input
  device to the computer
• Output stream: sequence of characters from the
  computer to an output device

                                                             3
I/O Streams and Standard I/O Devices
                 (cont.)
• Use iostream header file to extract
  (receive) data from keyboard and send output
  to the screen
  – Contains definitions of two data types:
     • istream - input stream
     • ostream - output stream
  – Has two variables:
     • cin - stands for common input
     • cout - stands for common output

                                              4
I/O Streams and Standard I/O Devices
                 (cont.)
• To use cin and cout, the preprocessor
  directive #include <iostream> must be
  used
• Variable declaration is similar to:
  – istream cin;
  – ostream cout;
• Input stream variables: type istream
• Output stream variables: type ostream
                                          5
cin and the Extraction Operator >>
• The syntax of an input statement using cin
  and the extraction operator >> is:


• The extraction operator >> is binary
  – Left-side operand is an input stream variable
     • Example: cin
  – Right-side operand is a variable


                                                    6
cin and the Extraction Operator >>
              (continued)
• No difference between a single cin with
  multiple variables and multiple cin
  statements with one variable
• When scanning, >> skips all whitespace
  – Blanks and certain nonprintable characters
• >> distinguishes between character 2 and
  number 2 by the right-side operand of >>
  – If type char or int (or double), the 2 is treated
    as a character or as a number 2

                                                    7
cin and the Extraction Operator >>
           (continued)




                                     8
Output and Formatting Output
• Syntax of cout when used with <<


• Expression is evaluated
• Value is printed
• Manipulator is used to format the output
  – Example: endl
                                             9
setprecision Manipulator
• Syntax:


• Outputs decimal numbers with up to n
  decimal places
• Must include the header file iomanip:
  – #include <iomanip>



                                          10
fixed Manipulator
• fixed outputs floating-point numbers in a
  fixed decimal format
  – Example: cout << fixed;
  – Disable by using the stream member function
    unsetf
     • Example: cout.unsetf(ios::fixed);
• The manipulator scientific is used to
  output floating-point numbers in scientific
  format

                                                  11
showpoint Manipulator
• showpoint forces output to show the
  decimal point and trailing zeros
• Examples:
  – cout << showpoint;
  – cout << fixed << showpoint;




                                        12
setw
• Outputs the value of an expression in specific
  columns
  – cout << setw(5) << x << endl;
• If number of columns exceeds the number of
  columns required by the expression
  – Output of the expression is right-justified
  – Unused columns to the left are filled with spaces
• Must include the header file iomanip


                                                        13
setfill Manipulator
• Output stream variables can use setfill to
  fill unused columns with a character



• Example:
  – cout << setfill('#');



                                           14
left and right Manipulators
• left: left-justifies the output


• Disable left by using unsetf


• right: right-justifies the output


                                      15
Summary
• Stream: infinite sequence of characters from a
  source to a destination
• Input stream: from a source to a computer
• Output stream: from a computer to a
  destination
• cin: common input
• cout: common output
• To use cin and cout, include iostream
  header
                                               16
Summary (continued)
• The manipulators setprecision, fixed,
  showpoint, setw, setfill, left, and right
  can be used for formatting output
• Include iomanip for the manipulators
  setprecision, setw, and setfill


Source:
C++ Programming: From Problem Analysis to Program
  Design, Fourth Edition

                                                    17

More Related Content

What's hot

What's hot (20)

Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Tokens in C++
Tokens in C++Tokens in C++
Tokens in C++
 
Character set of c
Character set of cCharacter set of c
Character set of c
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
Variables in C++, data types in c++
Variables in C++, data types in c++Variables in C++, data types in c++
Variables in C++, data types in c++
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
c++ lab manual
c++ lab manualc++ lab manual
c++ lab manual
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
C fundamental
C fundamentalC fundamental
C fundamental
 
C presentation book
C presentation bookC presentation book
C presentation book
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Programming Fundamental Presentation
Programming Fundamental PresentationProgramming Fundamental Presentation
Programming Fundamental Presentation
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
Structure in c
Structure in cStructure in c
Structure in c
 
Function in C
Function in CFunction in C
Function in C
 

Viewers also liked

Input and output in c
Input and output in cInput and output in c
Input and output in c
Rachana Joshi
 
file handling c++
file handling c++file handling c++
file handling c++
Guddu Spy
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
웅식 전
 

Viewers also liked (20)

Managing console
Managing consoleManaging console
Managing console
 
C++ io manipulation
C++ io manipulationC++ io manipulation
C++ io manipulation
 
Managing console input and output
Managing console input and outputManaging console input and output
Managing console input and output
 
Manipulators
ManipulatorsManipulators
Manipulators
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
Console Io Operations
Console Io OperationsConsole Io Operations
Console Io Operations
 
Input and output in c
Input and output in cInput and output in c
Input and output in c
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
 
Files in c++
Files in c++Files in c++
Files in c++
 
file handling c++
file handling c++file handling c++
file handling c++
 
Vcs26
Vcs26Vcs26
Vcs26
 
Unit iv
Unit ivUnit iv
Unit iv
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basics
 
Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
 

Similar to Formatted input and output

FILE OPERATIONS.pptx
FILE OPERATIONS.pptxFILE OPERATIONS.pptx
FILE OPERATIONS.pptx
DeepasCSE
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
Ronaldo Aditya
 

Similar to Formatted input and output (20)

C++ Ch3
C++ Ch3C++ Ch3
C++ Ch3
 
Object oriented programming 14 output formatting using manipulators
Object oriented programming 14 output formatting using manipulatorsObject oriented programming 14 output formatting using manipulators
Object oriented programming 14 output formatting using manipulators
 
Object oriented programming 13 input stream and devices in cpp
Object oriented programming 13 input stream and devices in cppObject oriented programming 13 input stream and devices in cpp
Object oriented programming 13 input stream and devices in cpp
 
FILE OPERATIONS.pptx
FILE OPERATIONS.pptxFILE OPERATIONS.pptx
FILE OPERATIONS.pptx
 
Basic Elements of C++
Basic Elements of C++Basic Elements of C++
Basic Elements of C++
 
Chapter 3 malik
Chapter 3 malikChapter 3 malik
Chapter 3 malik
 
Chapter 3 malik
Chapter 3 malikChapter 3 malik
Chapter 3 malik
 
Chapter 3 malik
Chapter 3 malikChapter 3 malik
Chapter 3 malik
 
C++ ch2
C++ ch2C++ ch2
C++ ch2
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
C++ basic.ppt
C++ basic.pptC++ basic.ppt
C++ basic.ppt
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
 
1-19 CPP Slides 2022-02-28 18_22_ 05.pdf
1-19 CPP Slides 2022-02-28 18_22_ 05.pdf1-19 CPP Slides 2022-02-28 18_22_ 05.pdf
1-19 CPP Slides 2022-02-28 18_22_ 05.pdf
 
Iostream in c++
Iostream in c++Iostream in c++
Iostream in c++
 
Input and Output
Input and OutputInput and Output
Input and Output
 
c-introduction.pptx
c-introduction.pptxc-introduction.pptx
c-introduction.pptx
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
Manipulators
ManipulatorsManipulators
Manipulators
 

More from Online

More from Online (20)

Philosophy of early childhood education 3
Philosophy of early childhood education 3Philosophy of early childhood education 3
Philosophy of early childhood education 3
 
Philosophy of early childhood education 2
Philosophy of early childhood education 2Philosophy of early childhood education 2
Philosophy of early childhood education 2
 
Philosophy of early childhood education 1
Philosophy of early childhood education 1Philosophy of early childhood education 1
Philosophy of early childhood education 1
 
Philosophy of early childhood education 4
Philosophy of early childhood education 4Philosophy of early childhood education 4
Philosophy of early childhood education 4
 
Operation and expression in c++
Operation and expression in c++Operation and expression in c++
Operation and expression in c++
 
Functions
FunctionsFunctions
Functions
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
 
Introduction to problem solving in c++
Introduction to problem solving in c++Introduction to problem solving in c++
Introduction to problem solving in c++
 
Optical transmission technique
Optical transmission techniqueOptical transmission technique
Optical transmission technique
 
Multi protocol label switching (mpls)
Multi protocol label switching (mpls)Multi protocol label switching (mpls)
Multi protocol label switching (mpls)
 
Lan technologies
Lan technologiesLan technologies
Lan technologies
 
Introduction to internet technology
Introduction to internet technologyIntroduction to internet technology
Introduction to internet technology
 
Internet standard routing protocols
Internet standard routing protocolsInternet standard routing protocols
Internet standard routing protocols
 
Internet protocol
Internet protocolInternet protocol
Internet protocol
 
Application protocols
Application protocolsApplication protocols
Application protocols
 
Addressing
AddressingAddressing
Addressing
 
Transport protocols
Transport protocolsTransport protocols
Transport protocols
 
Leadership
LeadershipLeadership
Leadership
 
Introduction to management
Introduction to managementIntroduction to management
Introduction to management
 

Recently uploaded

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 

Formatted input and output

  • 2. Objectives In this chapter, you will: • Learn what a stream is and examine input and output streams • Explore how to read data from the standard input device • Learn how to use predefined functions in a program • Learn how to write data to the standard output device • Discover how to use manipulators in a program to format output 2
  • 3. I/O Streams and Standard I/O Devices • I/O: sequence of bytes (stream of bytes) from source to destination – Bytes are usually characters, unless program requires other types of information • Stream: sequence of characters from source to destination • Input stream: sequence of characters from an input device to the computer • Output stream: sequence of characters from the computer to an output device 3
  • 4. I/O Streams and Standard I/O Devices (cont.) • Use iostream header file to extract (receive) data from keyboard and send output to the screen – Contains definitions of two data types: • istream - input stream • ostream - output stream – Has two variables: • cin - stands for common input • cout - stands for common output 4
  • 5. I/O Streams and Standard I/O Devices (cont.) • To use cin and cout, the preprocessor directive #include <iostream> must be used • Variable declaration is similar to: – istream cin; – ostream cout; • Input stream variables: type istream • Output stream variables: type ostream 5
  • 6. cin and the Extraction Operator >> • The syntax of an input statement using cin and the extraction operator >> is: • The extraction operator >> is binary – Left-side operand is an input stream variable • Example: cin – Right-side operand is a variable 6
  • 7. cin and the Extraction Operator >> (continued) • No difference between a single cin with multiple variables and multiple cin statements with one variable • When scanning, >> skips all whitespace – Blanks and certain nonprintable characters • >> distinguishes between character 2 and number 2 by the right-side operand of >> – If type char or int (or double), the 2 is treated as a character or as a number 2 7
  • 8. cin and the Extraction Operator >> (continued) 8
  • 9. Output and Formatting Output • Syntax of cout when used with << • Expression is evaluated • Value is printed • Manipulator is used to format the output – Example: endl 9
  • 10. setprecision Manipulator • Syntax: • Outputs decimal numbers with up to n decimal places • Must include the header file iomanip: – #include <iomanip> 10
  • 11. fixed Manipulator • fixed outputs floating-point numbers in a fixed decimal format – Example: cout << fixed; – Disable by using the stream member function unsetf • Example: cout.unsetf(ios::fixed); • The manipulator scientific is used to output floating-point numbers in scientific format 11
  • 12. showpoint Manipulator • showpoint forces output to show the decimal point and trailing zeros • Examples: – cout << showpoint; – cout << fixed << showpoint; 12
  • 13. setw • Outputs the value of an expression in specific columns – cout << setw(5) << x << endl; • If number of columns exceeds the number of columns required by the expression – Output of the expression is right-justified – Unused columns to the left are filled with spaces • Must include the header file iomanip 13
  • 14. setfill Manipulator • Output stream variables can use setfill to fill unused columns with a character • Example: – cout << setfill('#'); 14
  • 15. left and right Manipulators • left: left-justifies the output • Disable left by using unsetf • right: right-justifies the output 15
  • 16. Summary • Stream: infinite sequence of characters from a source to a destination • Input stream: from a source to a computer • Output stream: from a computer to a destination • cin: common input • cout: common output • To use cin and cout, include iostream header 16
  • 17. Summary (continued) • The manipulators setprecision, fixed, showpoint, setw, setfill, left, and right can be used for formatting output • Include iomanip for the manipulators setprecision, setw, and setfill Source: C++ Programming: From Problem Analysis to Program Design, Fourth Edition 17