Input, Output and Formatting Tools
Input, Output and Formatting Tools
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 (continued)
• 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
4
cin and the Extraction Operator
>>
• The syntax of an input statement using cin
and the extraction operator >> is:
5
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
6
cin and the Extraction Operator
>> (continued)
7
cin and the Extraction Operator
>> (continued)
• 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
8
cin and the Extraction Operator
>> (continued)
9
cin and the get Function
• The get function
− Inputs next character (including whitespace)
− Stores in memory location indicated by its
argument
• The syntax of cin and the get function:
varChar
− Is a char variable
− Is the argument (parameter) of the function
11
The Dot Notation Between I/O
Stream Variables and I/O Functions
• In the statement
cin.get(ch);
cin and get are two separate identifiers
separated by a dot
• Dot separates the input stream variable name
from the member, or function, name
• In C++, dot is the member access operator
12
Input Failure
13
Output and Formatting Output
• Expression is evaluated
• Value is printed
• Manipulator is used to format the output
− Example: endl
14
setprecision Manipulator
• Syntax:
15
fixed Manipulator
16
showpoint Manipulator
17
setw
18
Additional Output Formatting
Tools
• Additional formatting tools that give you more
control over your output:
− setfill manipulator
− left and right manipulators
19
setfill Manipulator
• Example:
− cout << setfill('#');
20
left and right Manipulators
21
Types of Manipulators
• Two types of manipulators:
− With parameters
− Without parameters
• Parameterized: require iomanip header
− setprecision, setw, and setfill
• Nonparameterized: require iostream
header
− endl, fixed, showpoint, left, and flush
22
Input/Output and the string
Type
• An input stream variable (cin) and >>
operator can read a string into a variable of
the data type string
• Extraction operator
− Skips any leading whitespace characters and
reading stops at a whitespace character
• The function getline
− Reads until end of the current line
23
Summary