INPUT/
OUTPUT
In this chapter, you will:
Learn what a
Explore how to
stream is and
read data from
examine input
the standard
and output
input device
streams
I/O Streams and Standard I/O
Devices
• A program performs three basic operations: It gets data, it
manipulates the data, and it outputs the results.
• Various I/O operations can greatly enhance the flexibility of your
programs.
• In C++, I/O is a sequence of bytes, called a stream, from the
source to the destination.
• The bytes are usually characters, unless the program requires
other types of information, such as a graphic image or digital
speech.
I/O Streams and Standard I/O
Devices
• Therefore, a stream is a sequence of characters from the
source to the destination. There are two types of streams:
• Input stream: A sequence of characters from an input
device to the computer.
• Output stream: A sequence of characters from the
computer to an output device.
• Recall that the standard input device is
I/O usually the keyboard, and the standard
output device is usually the screen.
Streams • To receive data from the keyboard and send
output to the screen, every C++ program
and must use the header file iostream.
• This header file contains, among other
Standard things, the definitions of two data types,
istream (input stream) and ostream
(output stream).
I/O • The header file also contains two variable
declarations,one for cin (pronounced ‘‘see-
Devices in’’), which stands for common input, and one
for cout (pronounced ‘‘see-out’’), which
stands for common output.
I/O Streams and Standard I/O
Devices
To use cin and
cout, every C++
#include
program must use
<iostream>
the preprocessor
directive:
• In C++, cout sends formatted output to
standard output devices, such as the
C++ Output screen. We use the cout object along with
the << operator for displaying output.
• Note: If we don't include the
using namespace std; statement,
C++ Output we need to use std::cout
instead of cout.
• In C++, cin takes formatted
input from standard input
C++ Input devices such as the keyboard.
We use the cin object along with
the >> operator for taking input.
C++ Taking Multiple Inputs
• Write a program that accepts as
input the mass, in grams, and
density, in grams per cubic
EXERCISE centimeters, and outputs the
volume of the object using the
formula: volume = mass / density.