SlideShare a Scribd company logo
1 of 41
Download to read offline
Introduction to Computer Programming
Input and Output
C++ Programming: From Problem Analysis to Program Design
1
Chapter Topics
• I/O Streams, Devices
• Predefined Functions
• Input Failure
• Formatting Output
• Formatting Tools
2
I/O Streams and Devices
• Input stream
– A sequence of characters/bytes
– From an input device
– Into the computer
• Output stream
– A sequence of characters/bytes
– From the computer
– To an output device
3
Input Streams
4
• Input data is considered to be an endless sequence
of characters/bytes coming into a program from an
input device (keyboard, file, etc.)
1 4 19 34 HI MOM. 7 ...
cin and the Extraction Operator >>
• A binary operator
– Takes two operands
– Name of an input stream on the left
• cin for standard input from keyboard
– Variable on the right
• Variables can be "cascaded"
cin >> amount >> count >> direction;
• Variables should generally be simple types
5
The Extraction Operator >>
6
• Enables you to do input with the cin command
• Think of the >> as pointing to where the data will
end up
• C++ able to handle different types of data and
multiple inputs correctly
The Reading Marker
7
1 4 19 34 HI MOM. 7 ...
• Keeps track of point in the input stream where the
computer should continue reading
• Extraction >> operator leaves reading marker
following last piece of data read
The Reading Marker
• During execution of a cin command
– as long as it keeps finding data, it keeps reading
– when the reading marker hits something not data, it quits
reading
• Things in the input stream that cin considers not data
– spaces
– tab t
– newline character n
(pressing the RETURN key)
– for numeric input, something nonnumeric
8
Input to a Simple Variable
9
• char
– Skips any white space characters in the beginning (leading
white spaces)
– Reads one character
– Any other characters in the stream are held for later input
• int
– Skips leading white space
– Reads leading + or ‐
– Reads numerals
– Quits reading when it hits non numeral (letters, symbols,
spaces, tabs, new line)
Input to a Simple Variable
10
• double (or float)
– Skips leading white space
– Reads leading + or –
– Reads numerals and at most one decimal point
– Quits reading when it hits something not numeric
11
Example
Example
12
13
Example
Predefined Functions
14
• Function in a computer language is similar to concept
of a function in mathematics
– The function is sent value(s)
• Called "arguments" or "parameters"
– It manipulates the value
– It returns a value
• Some functions "do a task"
Reading cString Data with cin
15
• Keyboard response of two words (separated by a
space) causes the cin command to quit reading
– the space is considered nondata (in spite of our intent)
???
Example
16
Reading cString Data with cin.get function
• cin.get reads white spaces
• Syntax:
cin.get(varChar);
• Example
cin.get (chVal);
–chVal is the parameter (name of variable)
– the .get function retrieves a character from the
keyboard
– stores the character in chVal
17
Name of cstring
Example
18
Reading cString Data with getline function
19
• The getline ( ) function will allow the
programmer to access all the characters
including white spaces
char Array variable Length (max number
of characters)
Character which
terminates read
Example
20
cin and the ignore Function
21
• Syntax:
cin.ignore (intValue, charVal);
• Example:
cin.ignore (10,'n');
• The ignore ignore function causes characters in
the input stream to be ignored (discarded)
– In this example for 10 characters … or …
– Until a newline character occurs
– It also discards the newline character
The ignore and
get also work for
other input streams
(such as file input
streams)
Example
22
Example
23
Using the getline( )
24
• Problem : the getline( ) quits reading when it
finds a newline
– Suppose you have terminated previous input with the
<RETURN> key (newline still in input stream)
– getline ( ) finds the newline immediately and
declares its task finished
– we must somehow discard the newline in the input stream
???
Using the ignore( )
25
• Solution: the ignore( ) command
• Tells the program to skip either the next 10 characters
or until it reaches a newline
– whichever comes first
• This effectively discards the newline
The clear Function
26
• Use the clear to return the input stream to a working
state
• Example:
cin.clear();
cin.ignore (200,'n');
// to empty out input stream
Example
27
28
Example
Input Failure
29
• Happens when value in the input stream is invalid for
the variable
• Example:
int x, y;
cin >> x >> y; // Enter B 37
– Value of 'B' not valid for an int
When an input stream fails,
system ignores all further I/O
Formatting Output
30
• Producing proper output in the proper format is
important
– Specify decimal precision
– Specify left or right justification
– Align columns of numbers
• C++ provides I/O manipulators
• Syntax:
cout << manipulator << expression …
Manipulators
31
• Must first of all
#include <iomanip>
• For decimal precision use
cout << setprecision (n) << …
• To output floating point numbers in fixed decimal
format use
cout << fixed << …
• To force decimal zeros to show
cout << showpoint << …
Manipulators
32
• To specify right justification in a specified number of blanks use
cout << setw(n) << …
• If the number of blanks required to print the expression
exceeds specified size
– Size is ignored
• Problem – print series of names left justified followed by right
justified numbers
Osgood Smart 1.23
Joe Schmo 456.78
• Names are of
different length
• Need variable
number of spaces
Manipulators
33
• Print name, then variable number of spaces using the
setw( )
• Example
cout << showpoint << fixed ;
cout << name <<
setw(25 - strlen(name))<<" ";
cout << setw (8) <<
setprecision(2) << amt;
Example
34
Example
35
Formatting Tools
36
• Possible to specify a character to fill leading spaces
using the cout.fill () or setfill( )
• Example
cout.fill ('*');
cout << setw(10) <<
setprecision(2);
cout << pmtAmount ;
• Result
*****12.34
Example
37
38
Example
39
Example
40
Example
41
Example

More Related Content

Similar to Input and Output

Object oriented programming 8 basics of c++ programming
Object oriented programming 8 basics of c++ programmingObject oriented programming 8 basics of c++ programming
Object oriented programming 8 basics of c++ programmingVaibhav Khanna
 
Learning python
Learning pythonLearning python
Learning pythonFraboni Ec
 
Learning python
Learning pythonLearning python
Learning pythonJames Wong
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteachingeteaching
 
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxINPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxjaggernaoma
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and outputOnline
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxRonaldo Aditya
 
FILE OPERATIONS.pptx
FILE OPERATIONS.pptxFILE OPERATIONS.pptx
FILE OPERATIONS.pptxDeepasCSE
 

Similar to Input and Output (20)

Object oriented programming 8 basics of c++ programming
Object oriented programming 8 basics of c++ programmingObject oriented programming 8 basics of c++ programming
Object oriented programming 8 basics of c++ programming
 
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
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
Learning python
Learning pythonLearning python
Learning python
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxINPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
 
C++ basic.ppt
C++ basic.pptC++ basic.ppt
C++ basic.ppt
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
FILE OPERATIONS.pptx
FILE OPERATIONS.pptxFILE OPERATIONS.pptx
FILE OPERATIONS.pptx
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 

More from Jason J Pulikkottil

Overview of computers and programming
Overview of computers and programmingOverview of computers and programming
Overview of computers and programmingJason J Pulikkottil
 
Repetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniquesRepetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniquesJason J Pulikkottil
 
Computer Architecture Performance and Energy
Computer Architecture Performance and EnergyComputer Architecture Performance and Energy
Computer Architecture Performance and EnergyJason J Pulikkottil
 
ARM 7 and 9 Core Architecture Illustration
ARM 7 and 9 Core Architecture IllustrationARM 7 and 9 Core Architecture Illustration
ARM 7 and 9 Core Architecture IllustrationJason J Pulikkottil
 
Microprocessors and Microcontrollers 8086 Pin Connections
Microprocessors and Microcontrollers 8086 Pin ConnectionsMicroprocessors and Microcontrollers 8086 Pin Connections
Microprocessors and Microcontrollers 8086 Pin ConnectionsJason J Pulikkottil
 
Basic Electronics Theory and Components
Basic Electronics Theory and ComponentsBasic Electronics Theory and Components
Basic Electronics Theory and ComponentsJason J Pulikkottil
 
Deep Learning for Health Informatics
Deep Learning for Health InformaticsDeep Learning for Health Informatics
Deep Learning for Health InformaticsJason J Pulikkottil
 

More from Jason J Pulikkottil (12)

Overview of computers and programming
Overview of computers and programmingOverview of computers and programming
Overview of computers and programming
 
Repetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniquesRepetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniques
 
Selection
SelectionSelection
Selection
 
Basic Elements of C++
Basic Elements of C++Basic Elements of C++
Basic Elements of C++
 
Computer Architecture Performance and Energy
Computer Architecture Performance and EnergyComputer Architecture Performance and Energy
Computer Architecture Performance and Energy
 
ARM 7 and 9 Core Architecture Illustration
ARM 7 and 9 Core Architecture IllustrationARM 7 and 9 Core Architecture Illustration
ARM 7 and 9 Core Architecture Illustration
 
Microprocessors and Microcontrollers 8086 Pin Connections
Microprocessors and Microcontrollers 8086 Pin ConnectionsMicroprocessors and Microcontrollers 8086 Pin Connections
Microprocessors and Microcontrollers 8086 Pin Connections
 
Basic Electronics Theory and Components
Basic Electronics Theory and ComponentsBasic Electronics Theory and Components
Basic Electronics Theory and Components
 
Electronic Circuits
Electronic CircuitsElectronic Circuits
Electronic Circuits
 
Power Amplifier
Power AmplifierPower Amplifier
Power Amplifier
 
E12 Resistor Series
E12 Resistor SeriesE12 Resistor Series
E12 Resistor Series
 
Deep Learning for Health Informatics
Deep Learning for Health InformaticsDeep Learning for Health Informatics
Deep Learning for Health Informatics
 

Recently uploaded

Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 

Recently uploaded (20)

Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 

Input and Output

  • 1. Introduction to Computer Programming Input and Output C++ Programming: From Problem Analysis to Program Design 1
  • 2. Chapter Topics • I/O Streams, Devices • Predefined Functions • Input Failure • Formatting Output • Formatting Tools 2
  • 3. I/O Streams and Devices • Input stream – A sequence of characters/bytes – From an input device – Into the computer • Output stream – A sequence of characters/bytes – From the computer – To an output device 3
  • 4. Input Streams 4 • Input data is considered to be an endless sequence of characters/bytes coming into a program from an input device (keyboard, file, etc.) 1 4 19 34 HI MOM. 7 ...
  • 5. cin and the Extraction Operator >> • A binary operator – Takes two operands – Name of an input stream on the left • cin for standard input from keyboard – Variable on the right • Variables can be "cascaded" cin >> amount >> count >> direction; • Variables should generally be simple types 5
  • 6. The Extraction Operator >> 6 • Enables you to do input with the cin command • Think of the >> as pointing to where the data will end up • C++ able to handle different types of data and multiple inputs correctly
  • 7. The Reading Marker 7 1 4 19 34 HI MOM. 7 ... • Keeps track of point in the input stream where the computer should continue reading • Extraction >> operator leaves reading marker following last piece of data read
  • 8. The Reading Marker • During execution of a cin command – as long as it keeps finding data, it keeps reading – when the reading marker hits something not data, it quits reading • Things in the input stream that cin considers not data – spaces – tab t – newline character n (pressing the RETURN key) – for numeric input, something nonnumeric 8
  • 9. Input to a Simple Variable 9 • char – Skips any white space characters in the beginning (leading white spaces) – Reads one character – Any other characters in the stream are held for later input • int – Skips leading white space – Reads leading + or ‐ – Reads numerals – Quits reading when it hits non numeral (letters, symbols, spaces, tabs, new line)
  • 10. Input to a Simple Variable 10 • double (or float) – Skips leading white space – Reads leading + or – – Reads numerals and at most one decimal point – Quits reading when it hits something not numeric
  • 14. Predefined Functions 14 • Function in a computer language is similar to concept of a function in mathematics – The function is sent value(s) • Called "arguments" or "parameters" – It manipulates the value – It returns a value • Some functions "do a task"
  • 15. Reading cString Data with cin 15 • Keyboard response of two words (separated by a space) causes the cin command to quit reading – the space is considered nondata (in spite of our intent) ???
  • 17. Reading cString Data with cin.get function • cin.get reads white spaces • Syntax: cin.get(varChar); • Example cin.get (chVal); –chVal is the parameter (name of variable) – the .get function retrieves a character from the keyboard – stores the character in chVal 17 Name of cstring
  • 19. Reading cString Data with getline function 19 • The getline ( ) function will allow the programmer to access all the characters including white spaces char Array variable Length (max number of characters) Character which terminates read
  • 21. cin and the ignore Function 21 • Syntax: cin.ignore (intValue, charVal); • Example: cin.ignore (10,'n'); • The ignore ignore function causes characters in the input stream to be ignored (discarded) – In this example for 10 characters … or … – Until a newline character occurs – It also discards the newline character The ignore and get also work for other input streams (such as file input streams)
  • 24. Using the getline( ) 24 • Problem : the getline( ) quits reading when it finds a newline – Suppose you have terminated previous input with the <RETURN> key (newline still in input stream) – getline ( ) finds the newline immediately and declares its task finished – we must somehow discard the newline in the input stream ???
  • 25. Using the ignore( ) 25 • Solution: the ignore( ) command • Tells the program to skip either the next 10 characters or until it reaches a newline – whichever comes first • This effectively discards the newline
  • 26. The clear Function 26 • Use the clear to return the input stream to a working state • Example: cin.clear(); cin.ignore (200,'n'); // to empty out input stream
  • 29. Input Failure 29 • Happens when value in the input stream is invalid for the variable • Example: int x, y; cin >> x >> y; // Enter B 37 – Value of 'B' not valid for an int When an input stream fails, system ignores all further I/O
  • 30. Formatting Output 30 • Producing proper output in the proper format is important – Specify decimal precision – Specify left or right justification – Align columns of numbers • C++ provides I/O manipulators • Syntax: cout << manipulator << expression …
  • 31. Manipulators 31 • Must first of all #include <iomanip> • For decimal precision use cout << setprecision (n) << … • To output floating point numbers in fixed decimal format use cout << fixed << … • To force decimal zeros to show cout << showpoint << …
  • 32. Manipulators 32 • To specify right justification in a specified number of blanks use cout << setw(n) << … • If the number of blanks required to print the expression exceeds specified size – Size is ignored • Problem – print series of names left justified followed by right justified numbers Osgood Smart 1.23 Joe Schmo 456.78 • Names are of different length • Need variable number of spaces
  • 33. Manipulators 33 • Print name, then variable number of spaces using the setw( ) • Example cout << showpoint << fixed ; cout << name << setw(25 - strlen(name))<<" "; cout << setw (8) << setprecision(2) << amt;
  • 36. Formatting Tools 36 • Possible to specify a character to fill leading spaces using the cout.fill () or setfill( ) • Example cout.fill ('*'); cout << setw(10) << setprecision(2); cout << pmtAmount ; • Result *****12.34