COMP218 - Lesson 2
COMP218 - Lesson 2
Lesson 2
Example 1:
Lesson 2
Integer Operations:
int a = 1, b = 2, c = 3;
-
addition:
subtraction:
multiplication:
negation:
Lesson 2
a + 4 value is 1 + 4 = 5
c - 1 value is 3 - 1 = 2
b * c value is 2 * 3 = 6
- a value is -1
Lesson 2
Lesson 2
Identifiers used to name constants, variables and various other things as well;
Lesson 2
Case sensitive:
QUARTER, Quarter, quarter
are three different names
If variable name consists of more than one word, start each new word with an upper
case lette.r
Ex: muffinPrice, studentId
Lesson 2
Items in double quotes (") are treated as a message and shown exactly as they
appear;
Lesson 2
10
Memory Size
short
(short int)
int
long
(long int)
2 bytes
4 bytes
4 bytes
Size Range
-32,768 to 32,767
-2,147,483,648 to
2,147,483,647
-2,147,483,648 to
2,147,483,647
Sets the first variable equal to the first value entered in the keyboard, the second
variable equal to the second value entered, and so forth;
Program reads input only once user has pressed the <return> key;
Will skip over any number of line breaks or blanks until it finds the next input value.
Lesson 2
11
Slide 24:
2 cin - Some
S
Fac
cts (cont'd)
Possible formatss of input:
1) 10
0 11 12
Example
e:
int a, b, c;
cin >> a >> b >> c;
= <en
nter> key
= spacce
2) 10
0
11
12
3) 10
0 11
12
4) 10
0 11
12
0
5) 10
11
12
Slide 25:
2 Chara
acter Data Type
Charac
cter Obje
ects
A varriable of typ
pe char can
n store any single charracter value
e;
Chara
acter data in a C++ prrogram is re
epresented
d by writing the approp
priate symb
bol
betwe
een single quote
q
signss;
Exam
mple:
'a ' '#
#' 'Z ';
Decla
aring and in
nitializing a variable of type charaacter.
char letter = 'a';;
Lesson 2
12
2
26: Chara
acter Data Type
cter Obje
ects (contt'd)
e:
ade;
< "Enter
r your gr
rade: ";
> grade;
< "You received
r
< endl;
a grade of " << grade <<
espace cha
aracters = characterss that produ
uce empty (white) spa
ace when
such as bla
anks, new line characte
er and horizontal tabss.
27: Chara
acter Data Type
cter Obje
ects (contt'd)
that i, j are
e int variables and ch is a char vaariable.
ill the conte
ents of i , j and ch be after
a
input??
ent
Data
a
> i;
32
> i >> j;
4 60
6
or answer
rs.
video fo
13
3
28: Chara
acter Data Type
cter Obje
ects (contt'd)
> i >> ch;
25
25
A
25A
A
46 32
14
4
If you want the single quote ( ' ), character need to express it as \'
Example:
const char QUOTE = '\'';
\ (known as the backslash character) acts as an escape character in C++
In program
Effect
\'
'
\\
\n
new line
\t
tab character
\a
bell character
15
To begin a new line, use \n , when it can be made the last character of a string.
To begin a new line, use endl if the last output of a line is a value.
There are times when you do not want to skip over whitespace.
cin.get() (instead of cin >>..) reads the next input character regardless of whether
the next character is whitespace or not.
Example:
char nextCh;
cin.get(nextCh); // will read any character
Lesson 2
16
Slide 34:
3 Chara
acter Data Type
Input whitespa
ace Chara
acters (co
ont'd)
e:
Example
What is the input in
n each case
e?
char ch1
1, ch2, ch3;;
Check video fo
or answer
rs.
Slide 35:
3 Last Look
L
at Output
O
wiith int Ty
ype
cout can
c be used
d to outputt variables of
o type chaar , int and d
double;
cout with
w char outputs
o
justt that chara
acter;
cout with
w int displays just value
v
of inte
eger, using
g least amou
unt of spacce;
Can add
a whitesp
pace to output by writing setw(n)
n) (which is read "set w
width to n"));
NOTE
E: It only afffects the next item!
3 Last Look
L
at Output
O
wiith int Ty
ype (contt'd)
Slide 36:
Example
e:
int first=
=1, last = 9999;
9
cout <<
< setw( 6 ) << first <<
< last ;
Output:
(where
Lesson 2
19999
1
stands for a blank)
17
7
45 (qua
uarters)
107 (dim
mes)
3 (nic
ickels)
24 (pen
ennies)
video fo
or answer
r.
18
8
Whole
-12
123
Fraction
.352
.4
Lesson 2
19
Lesson 2
20
4.5
-0.375
10.6667
6.78e+007
523
5.23e+011
9.8e-005
When formatting output of real numbers, you need to consider the total number of
positions and the number of decimal places.
Lesson 2
21
endl *, fixed, showpoint, setw *, and setprecision are manipulators that can be used
to control output format.
endl is use to terminate the current output line and create blank lines in output.
If fixed has already been specified, argument n determines the number of places
displayed after the decimal point for floating point values.
Lesson 2
22
Lesson 2
23
Lesson 2
24
Slide 55:
5 Floatiing-Pointt Data Ty
ypes
Outputt Format for Real Numbers (cont'd
d)
Example
e 3:
What ou
utput is gen
nerated by the
t followin
ng program
m segment?
double a = 123.545;
//cout << fixe
ed << sho
owpoint;
cout << setw(8) << set
tprecisio
on(3) << a << end
dl;
Slide 56:
5 Floatiing-Pointt Data Ty
ypes
Manipu
ulators/H
Header Files
Lesson 2
25
5
Slide 57:
5 Arithm
metic Operators
Operattors
+, -, *, /, % are
e called ope
erators.
Prece
edence for the
t operato
ors we have
e seen to daate:
1. ( )
2. Un
nary + or 3. * / % from le
eft to right
4. + - from left to
t right
5. <<
< >> stream
m insertion//extraction
6. = single
s
assig
gnment stattement
Slide 58:
5 Arithm
metic Operators
Operattors (con
nt'd)
Some
e examples::
1) 2 + 3 * 5 - 5 * 2 / 5 + 10
1 (25)
2) (2 + 3) * 5 - 5 * (2 / 5) + 10 (35)
5)
3) (2 + 3) * 5 - (5 * (2 / 5)) + 10) (15
Write
e as a C++ statement:
Lesson 2
26
6
Example 1:
What will be stored in r ?
double r;
int a = 4, b = 2;
r = a/b;
Check video for answers.
Lesson 2
27
28
Lesson 2
29
= 20;
= 4;
+= b; // a = ______
*= a; // b = ______
%= b + 6; // a = ______
Lesson 2
30
Declaring a string:
string fName, lName;
Can use cin, cout with the << and >> operators.
Example 1:
cin >> fName >>
cout << fName <<
cout << setw(10)
<< setw(10)
lName;
lName << endl;
<< fName
<< lName << endl;
Lesson 2
31
70: String
g Data Ty
ypes
ple 2
ude <iost
tream> //
/ for cin
n, cout
ude <ioma
anip> // for setw
w()
ude <stri
ing> // for
f
strin
ng
namespac
ce std;
ain()
Declarati
ion of va
ariables
ng teamN
Name, coa
ach;
teamID; // leagu
ue assign
ns teamID
D #
r level; // teams
s play at
t level A
A, B, C
Request input
i
fro
om user
<< "Ent
ter team name, id
d, level and coac
ch: " << endl;
>> teamN
Name >> teamID
t
>>
> level >
>> coach;
;
32
2
71: String
g Data Ty
ypes
ple 2 (con
nt'd)
<
<
<
<
"Team: "
setw(10)
setw(10)
setw(10)
n 0;
<<
<<
<<
<<
te
eamName <<
< endl;
te
eamID << " (ID)" << endl
le
evel << " (Level) " << end
dl
co
oach << " (Coach) " << end
dl;
72: String
g Data Ty
ypes
tions
Concatena
ation:
pending of one
o string onto
o
the en
nd of anotheer.
e:
g word1 = "Hello", word2 = "World
d", mess;
;
= word1 + word2;
33
3
Function find() returns an unsigned integer value that is the beginning position for
the first occurrence of a particular substring within the string.
The substring argument can be a string constant, a string expression or a char value.
If the substring was not found, function find returns the special value string::npos.
Lesson 2
34
The first argument is an unsigned integer that specifies a starting position within the
string.
The second argument is an unsigned integer that specifies the length of the desired
substring.
Positions of characters within a string are numbered starting from 0, not from 1.
Lesson 2
35