SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Types of function call
Types of function callTypes of function call
Types of function call
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Data types
Data typesData types
Data types
 
Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)Dynamic Memory Allocation(DMA)
Dynamic Memory Allocation(DMA)
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Constant, variables, data types
Constant, variables, data typesConstant, variables, data types
Constant, variables, data types
 
Enums in c
Enums in cEnums in c
Enums in c
 
Type conversion
Type conversionType conversion
Type conversion
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming Language
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Strings in C
Strings in CStrings in C
Strings in C
 
Storage classes in c++
Storage classes in c++Storage classes in c++
Storage classes in c++
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
Basic data types in python
Basic data types in pythonBasic data types in python
Basic data types in python
 

Destacado

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
웅식 전
 

Destacado (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
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
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++
 
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 a 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 a 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
 

Más de Online

Más de 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
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
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
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
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
 

Último (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
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
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
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
 

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