SlideShare uma empresa 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

Mais conteúdo relacionado

Mais procurados

Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++Tech_MX
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and StringTasnima Hamid
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and UnionsDhrumil Patel
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programmingAppili Vamsi Krishna
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationRabin BK
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C ProgrammingDevoAjit Gupta
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
Strings in C language
Strings in C languageStrings in C language
Strings in C languageP M Patil
 
Types of function call
Types of function callTypes of function call
Types of function callArijitDhali
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationViji B
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 

Mais procurados (20)

Dynamic memory allocation in c++
Dynamic memory allocation in c++Dynamic memory allocation in c++
Dynamic memory allocation in c++
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
File handling in C
File handling in CFile handling in C
File handling in C
 
Unions in c
Unions in cUnions in c
Unions in c
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Structure in c
Structure in cStructure in c
Structure in c
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Types of function call
Types of function callTypes of function call
Types of function call
 
C functions
C functionsC functions
C functions
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 

Destaque (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++
 
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++
 

Semelhante 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
 

Semelhante 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
 

Mais 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
 

Mais 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

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
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
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
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
 
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)

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
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
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
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
 
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