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

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 

Recently uploaded (20)

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
★ 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
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 

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