SlideShare a Scribd company logo
1 of 28
Download to read offline
Basic Elements
&Expressions
T. Mohammad Ghaleb Aqeel
Academic year
2024-2023
L4
Basic Elements of C++
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
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
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
 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.)
 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.)
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.
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
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
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
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
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
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
‫البرنامج‬ ‫في‬ ‫ا‬ً‫ق‬‫مسب‬ ‫محددة‬ ‫وظائف‬ ‫استخدام‬
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
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.
•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
•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.
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
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);
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
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
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
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
cin and the ignore Function (cont’d.)
24
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);
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();
setw Manipulator: An Example
‫خالل‬
‫عملية‬
‫الترجمة‬
Compilation
‫قد‬
‫تظهر‬
‫اخطاء‬
‫في‬
‫صياغة‬
‫البرنامج‬
‫المصدر‬
‫ينبغي‬
‫على‬
‫المبرمج‬
‫تصحيحها‬
.
▪
‫هناك‬
‫ثالث‬
‫انواع‬
‫من‬
‫األخطاء‬
:
.1
‫اخطاء‬
‫في‬
‫قواعد‬
‫اللغة‬
:Syntax Errors
‫اخطاء‬
‫امالئية‬
‫في‬
‫كتابة‬
‫األوامر‬
.
.2
‫اخطاء‬
‫منطقية‬
Logical Errors
:
‫ال‬
‫يكتشفها‬
‫الحاسوب‬
‫وتظهر‬
‫عند‬
‫تنفيذ‬
‫البرنام‬
‫ج‬
‫على‬
‫عينه‬
‫من‬
‫البيانات‬
‫فنحصل‬
‫على‬
‫نتائج‬
‫خاطئه‬
‫او‬
‫غير‬
،‫متوقعة‬
‫ويقوم‬
‫الم‬
‫برمج‬
‫بتتبع‬
‫خطوات‬
‫البرنامج‬
‫لمعرفة‬
‫مصدر‬
‫الخطأ‬
‫وتصحيحه‬
‫وتسمى‬
‫هذه‬
‫العملية‬
Tracing
.
.3
‫اخطاء‬
‫اثناء‬
‫التشغيل‬
Run-Time Errors
:
‫تظهرعند‬
‫تنفيذ‬
‫البرنامج‬
‫مثل‬
‫عدم‬
‫حجز‬
‫مساحة‬
‫كافية‬
‫للمدخالت‬
‫او‬
‫الدخول‬
‫في‬
‫دوران‬
‫بال‬
،‫نهاية‬
‫وتظهر‬
‫رسالة‬
‫بنوع‬
‫الخط‬
‫اء‬
.
❑
‫تصحيح‬
‫األخطاء‬
Debugging

More Related Content

Similar to L4.pdf

CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
faithxdunce63732
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
ANUSUYA S
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
IIUM
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
IIUM
 

Similar to L4.pdf (20)

C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
 
Input and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequencesInput and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequences
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Chapter2
Chapter2Chapter2
Chapter2
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
 
Basic commands in C++
Basic commands in C++Basic commands in C++
Basic commands in C++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
C++ Unit 1PPT which contains the Introduction and basic o C++ with OOOps conc...
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 
Overloading of io stream operators
Overloading of io stream operatorsOverloading of io stream operators
Overloading of io stream operators
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
C++
C++C++
C++
 

Recently uploaded

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

L4.pdf

  • 1. Basic Elements &Expressions T. Mohammad Ghaleb Aqeel Academic year 2024-2023 L4 Basic Elements of C++
  • 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
  • 24. cin and the ignore Function (cont’d.) 24
  • 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();
  • 28. ‫خالل‬ ‫عملية‬ ‫الترجمة‬ Compilation ‫قد‬ ‫تظهر‬ ‫اخطاء‬ ‫في‬ ‫صياغة‬ ‫البرنامج‬ ‫المصدر‬ ‫ينبغي‬ ‫على‬ ‫المبرمج‬ ‫تصحيحها‬ . ▪ ‫هناك‬ ‫ثالث‬ ‫انواع‬ ‫من‬ ‫األخطاء‬ : .1 ‫اخطاء‬ ‫في‬ ‫قواعد‬ ‫اللغة‬ :Syntax Errors ‫اخطاء‬ ‫امالئية‬ ‫في‬ ‫كتابة‬ ‫األوامر‬ . .2 ‫اخطاء‬ ‫منطقية‬ Logical Errors : ‫ال‬ ‫يكتشفها‬ ‫الحاسوب‬ ‫وتظهر‬ ‫عند‬ ‫تنفيذ‬ ‫البرنام‬ ‫ج‬ ‫على‬ ‫عينه‬ ‫من‬ ‫البيانات‬ ‫فنحصل‬ ‫على‬ ‫نتائج‬ ‫خاطئه‬ ‫او‬ ‫غير‬ ،‫متوقعة‬ ‫ويقوم‬ ‫الم‬ ‫برمج‬ ‫بتتبع‬ ‫خطوات‬ ‫البرنامج‬ ‫لمعرفة‬ ‫مصدر‬ ‫الخطأ‬ ‫وتصحيحه‬ ‫وتسمى‬ ‫هذه‬ ‫العملية‬ Tracing . .3 ‫اخطاء‬ ‫اثناء‬ ‫التشغيل‬ Run-Time Errors : ‫تظهرعند‬ ‫تنفيذ‬ ‫البرنامج‬ ‫مثل‬ ‫عدم‬ ‫حجز‬ ‫مساحة‬ ‫كافية‬ ‫للمدخالت‬ ‫او‬ ‫الدخول‬ ‫في‬ ‫دوران‬ ‫بال‬ ،‫نهاية‬ ‫وتظهر‬ ‫رسالة‬ ‫بنوع‬ ‫الخط‬ ‫اء‬ . ❑ ‫تصحيح‬ ‫األخطاء‬ Debugging