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 (20)

Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Features of c language 1
Features of c language 1Features of c language 1
Features of c language 1
 
Enums in c
Enums in cEnums in c
Enums in c
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
C functions
C functionsC functions
C functions
 
Structure in C
Structure in CStructure in C
Structure in C
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Functions in C
Functions in CFunctions in C
Functions in C
 

Destacado

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
 
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 a Formatted input and output

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 manipulatorsVaibhav Khanna
 
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 cppVaibhav Khanna
 
FILE OPERATIONS.pptx
FILE OPERATIONS.pptxFILE OPERATIONS.pptx
FILE OPERATIONS.pptxDeepasCSE
 
Chapter 3 malik
Chapter 3 malikChapter 3 malik
Chapter 3 malikOshal Shah
 
Chapter 3 malik
Chapter 3 malikChapter 3 malik
Chapter 3 malikOshal Shah
 
Chapter 3 malik
Chapter 3 malikChapter 3 malik
Chapter 3 malikOshal Shah
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.pptFatimaZafar68
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxRonaldo Aditya
 
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.pdfdhruvjs
 
c-introduction.pptx
c-introduction.pptxc-introduction.pptx
c-introduction.pptxMangala R
 

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

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

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

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
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 ConsultingTechSoup
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
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).pptxVishalSingh1417
 
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 ClassesCeline George
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
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).pptxVishalSingh1417
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Último (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
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
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
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
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
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
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
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
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.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