SlideShare a Scribd company logo
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

Chapter 3 malik
Chapter 3 malikChapter 3 malik
Chapter 3 malik
Oshal Shah
 
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
sajjad ali khan
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
sachindane
 
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
 
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
ssuserf86fba
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
Umesh Nikam
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
msharshitha03s
 
Chapter2
Chapter2Chapter2
Chapter2
Anees999
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
Lakshmi Sarvani Videla
 
Basic commands in C++
Basic commands in C++Basic commands in C++
Basic commands in C++
Mujeeb UR Rahman
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
Bussines man badhrinadh
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
K Durga Prasad
 
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
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
Manzoor ALam
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
Pranali Chaudhari
 
Introduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOPIntroduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOP
UbaidKhan930128
 
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
 
Overloading of io stream operators
Overloading of io stream operatorsOverloading of io stream operators
Overloading of io stream operators
SARAVANAN GOPALAKRISHNAN
 

Similar to L4.pdf (20)

Chapter 3 malik
Chapter 3 malikChapter 3 malik
Chapter 3 malik
 
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++
 
Introduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOPIntroduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOP
 
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
 

Recently uploaded

مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
سمير بسيوني
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
deepaannamalai16
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
IsmaelVazquez38
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
melliereed
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
RamseyBerglund
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
Celine George
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
blueshagoo1
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
deepaannamalai16
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 

Recently uploaded (20)

مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
 
Electric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger HuntElectric Fetus - Record Store Scavenger Hunt
Electric Fetus - Record Store Scavenger Hunt
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 

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