0% found this document useful (0 votes)
76 views20 pages

3 PDF

This chapter discusses input/output streams and standard I/O devices, input failure, and using predefined functions in a program. It covers I/O streams as sequences of bytes from a source to a destination, and defines input and output streams. It also discusses the iostream header file, cin and cout variables, and the extraction operator >>. It describes how to handle input failure and introduces the concept of predefined functions organized in header files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views20 pages

3 PDF

This chapter discusses input/output streams and standard I/O devices, input failure, and using predefined functions in a program. It covers I/O streams as sequences of bytes from a source to a destination, and defines input and output streams. It also discusses the iostream header file, cin and cout variables, and the extraction operator >>. It describes how to handle input failure and introduces the concept of predefined functions organized in header files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Chapter 3:

Input/ Output
Outline
• In this chapter, you will study:
– I/O Streams and Standard I/O Devices
– Input Failure
– Using Predefined Functions in a Program

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’d.)
• Use iostream header file to 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’d.)
• Variable declaration is similar to:
– istream cin;
– ostream cout;
• To use cin and cout, the preprocessor directive
#include <iostream> must be used
• 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 >>
(cont’d.)
• 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 >>
(cont’d.)

• Entering a char value into an int or double


variable causes serious errors, called input failure

8
cin and the Extraction Operator >>
(cont’d.)
• When reading data into a char variable
– >> skips leading whitespace, finds and stores only the next
character
– Reading stops after a single character
• To read data into an int or double variable
– >> skips leading whitespace, reads + or - sign (if any),
reads the digits (including decimal)
– Reading stops on whitespace non-digit character

9
cin and the Extraction Operator >>
(cont’d.)

10
cin and the Extraction Operator >>
(cont’d.)

11
cin and the Extraction Operator >>
(cont’d.)

12
Input Failure
• Things can go wrong during execution
• If input data does not match corresponding
variables, program may run into problems
• Trying to read a letter into an int or double
variable will result in an input failure
• If an error occurs when reading data
– Input stream enters the fail state

C++ Programming: From Problem Analysis to Program Design, Sixth Edition


13
Input Failure (cont’d)

C++ Programming: From Problem Analysis to Program Design, Sixth Edition


14
Input Failure (cont’d)

C++ Programming: From Problem Analysis to Program Design, Sixth Edition


15
Using Predefined Functions in a
Program
• Function (subprogram): set of instructions
– When activated, it accomplishes a task
• main executes when a program is run
• Other functions execute only when called
• C++ includes a wealth of functions
– Predefined functions are organized as a collection of
libraries called header files

16
Using Predefined Functions in a
Program (cont’d.)
• Header file may contain several functions
• To use a predefined function, you need the name of
the appropriate header file
– You also need to know:
• Function name
• Number of parameters required
• Type of each parameter
• What the function is going to do

17
Using Predefined Functions in a
Program (cont’d.)
• To use pow (power), include cmath
– Two numeric parameters
– Syntax: pow(x,y) = xy
• x and y are the arguments or parameters
– In pow(2,3), the parameters are 2 and 3

18
Using Predefined Functions in a
Program (cont’d.)

19
Using Predefined Functions in a
Program (cont’d.)

20

You might also like