2. I/O Streams and Standard I/O
Devices
I/O stands for input/output.
An I/O Stream is a sequence of bytes
traveling from a source to a destination.
⚫ An input stream is a sequence of characters
from an input device (such as the keyboard)
to the computer.
⚫ An output stream is a sequence of characters
from the computer to an output device (such
as the monitor).
2
C++ Programming: From Problem
Analysis to Program Design, Seventh
Edition
3. I/O Streams and Standard I/O
Devices (cont’d.)
You must include the iostream header file in any
program that receives data from the keyboard or
sends output to the screen. You do this with the
preprocessor directive: #include <iostream>
⚫ This header file declares two important
variables:
cin (which stands for “common input”)
cout (which stands for “common output”)
⚫ The data types of these variables are
istream and ostream. These data types are
defined in the iostream header file.
3
C++ Programming: From Problem
Analysis to Program Design, Seventh
Edition
4. cin and
the Stream Extraction Operator >>
The extraction operator >> is the most
common way of reading a value from the
keyboard into a variable.
syntax:
The >> operator is a binary operator.
⚫ The left-side operand must be an input
stream variable, such as cin.
⚫ The right-side operand must be a declared
variable.
4
5. Example of a cin statement using >>:
int num1;
cin >> num1;
When this code executes, the program will
wait until the user types a value and
presses the Enter key. The program will
then assign the user’s value to the variable
named num1.
5
cin and
the Stream Extraction Operator >> (cont’d.)
6. There’s no difference between a
single cin with multiple variables
and multiple cin statements with
one variable each.
⚫ Example with one cin statement:
cin >> num1 >> num2;
⚫ Equivalent example with two cin
statements:
cin >> num1;
cin >> num2;
6
cin and
the Stream Extraction Operator >> (cont’d.)
7. The Extraction Operator and Various
Data Types
• The next few slides examine how the >>
operator behaves when you use it to read
values into variables of different data types
(char, int, double, string, …).
• In all of these cases, the >> operator skips
any leading whitespace.
• Recall that whitespace includes blanks, tabs,
and line returns.
• As we’ll see later, there are other ways of
reading in values that don’t skip leading
whitespace.
8. The Extraction Operator and
char data
When reading data into a char variable:
⚫ In C++, the extraction operator (>>) is
used for input operations, usually to
extract data from an input stream like
std::cin. It is often used to read user
input or read data from a file.
⚫ Reading stops after a single character
(واحد حرف بعد القراءة )تتوقف.
Example: suppose we’re executing this code:
char myChar;
cin >> myChar;
⚫ If the user types +23.47hello there! followed by
the Enter key, myChar will hold the value '+'.
8
9. The Extraction Operator and
int data
When reading data into an int variable:
⚫ >> skips leading whitespace, reads + or - sign
(if any), reads the digits.
⚫ Reading stops on any character that is not a
digit.( اًمرق ليس حرف أي على القراءة تتوقف
. )
Example: suppose we’re executing this code:
int myInt;
cin >> myInt;
⚫ If the user types -23.47hello there! followed
by the Enter key, myInt will hold the value
-23.
9
10. The Extraction Operator and
double data
When reading data into a double variable:
⚫ >> skips leading whitespace, reads + or - sign
(if any), reads the digits (including decimal
point).
⚫ Reading stops on any character that is not a
digit or the decimal point or e or E or + or -.
Example: suppose we’re executing this code:
double myDouble;
cin >> myDouble;
⚫ If the user types +23.47hello there!
followed by the Enter key, myDouble will hold
the value 23.47. 10
11. The Extraction Operator and
string data
When reading data into a string variable:
⚫ >> skips leading whitespace.
⚫ Reading stops at any whitespace
character ( بيضاء مسافة حرف أي عند القراءة تتوقف
. ).
Example: suppose we’re executing this code:
string myString;
cin >> myString;
⚫ If the user types +23.47hello there!
followed by the Enter key, myString will hold
the value "+23.47hello".
11
12. Input Failure اإلدخال فشل
An error called input failure can result if the user
enters data of a different type from the type
expected by the cin statement.
Example: suppose we’re executing this code:
int num1;
cin >> num1;
⚫ If the user types H followed by the Enter key, we’ll get an error
since the user did not enter an integer value.
As we’ll see later, C++ gives you tools to deal with this sort
of error.
12
13. Using Predefined Functions in a Program
Recall that a function is a named set of
instructions that accomplishes some task.
Recall also that the main function executes
when a program is run as main( )
Other functions execute only when called.
C++ includes a wealth of predefined functions.
⚫ These predefined functions are organized into the
header files (such as <iostream> and <string>) in the
C++ Standard Library.
13
البرنامج في اًقمسب محددة وظائف استخدام
14. Using Predefined Functions in
a Program (cont’d.)
To use a predefined function, you
must include the appropriate header
file.
⚫ You also need to know:
The function’s name
The number of parameters required
The type of each parameter
14
C++ Programming: From Problem
Analysis to Program Design, Seventh
Edition
15. Example of a Predefined
Function: pow
The pow function raises a number to a
power.
To use pow, include the cmath header file:
#include <cmath>
The pow function takes two numeric
parameters:
⚫ Syntax: pow(x,y)
This expression returns the value of xy.
x and y are the parameters.
In pow(2,3), the parameters are 2 and 3.
16. •As we’ll see in the weeks ahead, the syntax used in
calling pow on the previous slide is the same syntax
that we’ll use in calling other functions.
•In particular, the syntax consists of the
function’s name followed by zero or more
parameters enclosed in parentheses and separated
by commas.
•See the book’s Appendix F for other functions in
cmath, such as sqrt(x).
Function-Call Syntax
17. •The iostream header file defines many functions for
processing keyboard input, including:
•getLine
•get
•ignore
•putback
•peek
•clear
•Depending on what you're trying to do, you might need
to use these functions instead of (or in addition to) the
extraction operator >>.
Some Predefined Functions in
the iostream Header File
These two are the most useful.
18. cin and the getLine Function
Recall that the >> operator can read a
string into a variable of the string data
type, but that reading skips leading
whitespace and stops when it hits
whitespace after any text.
التشغيل عامل أن تذكر
>>
ف نصية سلسلة قراءة يمكنه
ي
تتخطى القراءة هذه لكن ،السلسلة بيانات نوع من متغير
مس إلى تصل عندما وتتوقف البادئة البيضاء المسافة
افة
نص أي بعد بيضاء
.
18
19. cin and the getLine Function
Alternatively, you can use the getline function.
⚫ It does not skip leading whitespace, as >> does.
⚫ And it reads until the end of the current line. It does not stop
reading when it hits whitespace, as >> does.
ذلك من ًالوبد
وظيفة استخدام يمكنك ،
getline.
⚫
يفعل كما ،البادئة البيضاء المسافة يتخطى وال
>>
.
نهاية حتى ويقرأ
السطر
الحالي
.
⚫
يفعل كما ،بيضاء مسافة إلى يصل عندما القراءة عن يتوقف وال
>>
.
Syntax:
where strVar is a string variable.
19
getline(cin, strVar);
20. cin and the getLine Function (cont’d.)
Recalling an earlier example, suppose we’re
executing this code:
string myString;
cin >> myString;
⚫ If the user types +23.47hello there! followed by the
Enter key, myString will hold the value "+23.47hello".
But if instead the code is :
string myString;
getline(cin, myString);
⚫ Now if the user types +23.47hello there!
followed by the Enter key, myString will hold
the value
" +23.47hello there!". 20
21. cin and the get Function
The get function:
⚫ Inputs the next character
(including whitespace).
⚫ Stores the character in the char
variable named as the function's
parameter.
The syntax of the get function:
where varChar is a char variable.
21
22. cin and the get Function (cont’d.)
Recalling an earlier example, suppose we’re
executing this code:
char myChar;
cin >> myChar;
⚫ If the user types +23.47hello there! followed by the
Enter key, myChar will hold the value '+'.
But if instead the code is :
char myChar;
cin.get(myChar);
⚫ Now if the user types +23.47hello there!
followed by the Enter key, myChar will hold the
value '+'.
22
23. cin and the ignore Function
The ignore function discards a portion of the input
stream.
Syntax:
– intExp is an integer expression.
– chExp is a char expression.
If intExp value's is m, the statement says to ignore the
next m characters or all characters up to and including
the character specified by chExp (whichever comes
first).
23
25. putback Function
The putback function places the
previous character extracted by
the get function from an input
stream back to that stream.
Syntax:
where varChar is a char variable.
25
C++ Programming: From Problem
Analysis to Program Design, Seventh
Edition
cin.putback(varChar);
26. peek Function
The peek function:
⚫ In C++, the peek function is a member
function of the std::istream class,
typically used with the std::cin object.
⚫ It allows you to peek at the next
character in the input stream without
actually extracting it.
⚫ Syntax:
where varChar is a char variable.
26
C++ Programming: From Problem
Analysis to Program Design, Seventh
Edition
varChar = cin.peek();