Title: Objectives
1Objectives
- Understand streamed input and output
2cout cin
include ltiostreamgt using namespace std int main
() int Length, Width, Area cout ltlt
Calculates the area of cout ltlt a
rectangle.\n
Calculating the area of a rectangle. _
3cout cin (cont)
cout ltlt What is the length? cin gtgt
Length cout ltlt What is the width? cin gtgt
Width Area Length Width
10
20
Calculating the area of a rectangle. _
What is the length? _
10
What is the width? _
20
200
4cout cin (cont)
cout ltlt The area of the cout ltlt
rectangle is ltlt Area ltlt .\n
10
20
Calculating the area of a rectangle. _
What is the length? _
10
What is the width? _
20
200
rectangle is 200.
The area of the
_
5cin Multiple Values
include ltiostreamgtusing namespace std int
main () int Length, Width, Area cout ltlt
Calculating the area of cout ltlt a
rectangle.\n
Calculating the area of a rectangle. _
6Multiple Values (cont)
cout ltlt Enter length and width of
ltlt rectangle.\n cout ltlt Separate entries
with ltlt a space cin gtgt Length
gtgtWidth
10
20
Calculating the area of a rectangle. _
Enter length and width of
rectangle.
Separate entries with a space _
10 20
7Multiple Values (cont)
Area Length Width cout ltlt The area of
the rectangle is ltlt Area ltlt
endl return 0
10
20
Calculating the area of a rectangle. _
Enter length and width of
rectangle.
Separate entries with a space _
10 20
200
The area of the rectangle is _
200
_
8Multiple values data types
include ltiostreamgtusing namespace std int
main () int Whole double Fractional
char Letter
9Multiple values data types
cout ltlt Enter an integer, a double,\n
ltlt and a character cin gtgt Whole gtgt
Fractional gtgt Letter
4
5.7
Enter an integer, a double, _
and a character _
4 5.7 b
b
10Multiple values data types
cout ltlt Whole ltlt Wholeltlt endl cout ltlt
Fractional ltlt Fractional ltlt
endl cout ltlt Letter ltlt Letter return 0
4
5.7
Enter an integer, a double,
and a character _
4 5.7 b
Whole 4
_
b
Fractional _
5.7
_
Letter b
11Incorrect data entry
cout ltlt Enter an integer, a double,\n
ltlt and a character cin gtgt Whole gtgt
Fractional gtgt Letter
Enter an integer, a double, _
and a character _
5.7 4 b
12cin gtgt Rules
- Integer read
- Ignores/skips leading white space characters
(space, tab, new line) - Begins collecting digits, stops at the first
non-digit character (leaving that character in
the buffer) - Character read
- Ignores/skips leading white space characters
(space, tab, new line) - Reads next character (any valid ASCII character)
13cin gtgt Rules
- String read
- Ignores/skips leading white space characters
(space, tab, new line) - Collects characters and stops at the first white
space character
14cin reads a string into a character array
include ltiostreamgtusing namespace std int
main () char Name15
Name
15cin reads a string into a character array
cout ltlt What is your name? cin gtgt Name
cout ltlt Good morning ltlt Name ltlt
endl return 0
What is your name? _
Charlie
Good morning Charlie
_
Name
C h a r l i e \0
16Read 2 strings into 2 character arrays
include ltiostreamgtusing namespace std int
main () char First10, Last10
172 strings into 2 character arrays (cont)
cout ltlt Enter your first ltlt and
last name\n and I will ltlt reverse
them.\n
Enter your first
and last name and I
will
reverse them. _
182 strings into 2 character arrays (cont)
cin gtgt First gtgt Last cout ltlt Last ltlt , ltlt
First ltlt endl return 0
Enter your first
and last name and I
will
reverse them. _
Johnny Jones
Jones, Johnny
J o h n n y \0
J o n e s \0
19The Typecast Operator
include ltiostream.gt int main () int Months,
Books double PerMonth cout ltlt How many books
do ltlt you plan to read? cin gtgt
Books cout ltlt How many months will
ltlt it take you to read them? cin gtgt
Months PerMonth static_cast ltdoublegt (Books) /
Months cout ltlt That is ltlt PerMonth ltlt
books per month.\n
20The Typecast Operator
include ltiostreamgt using namespace std int main
() int Number 65 cout ltlt Number ltlt endl
// C method of type casting cout ltlt
char(Number) ltlt endl
21 define Directive
include ltiostreamgt include ltcmathgt //needed
for pow functionusing namespace std define PI
3.14159 int main() double Area, Radius cout
ltlt This program calculates the ltltarea of a
circle.\n cout ltlt What is the radius of the
circle? cin gtgt Radius Area PI
pow(Radius, 2) cout ltlt The area is ltlt Area
22 define Directive
Include ltiostreamgt include ltcmathgt //needed
for pow functionusing namespace std define PI
3.14159 void main(void) double Area,
Radius cout ltlt This program calculates
the ltltarea of a circle.\n cout ltlt What
is the radius of the circle? cin gtgt
Radius Area PI pow(Radius, 2) cout ltlt
The area is ltlt Area
23Expressions with a value
cout ltlt (a 5)
24Expressions with a value
Assume a is 4 and b is 7
Statement Screen Output cout ltlt (a
b) 11 cout ltlt (b 2) 14 cout ltlt (b
(a2)) 15 cout ltlt (a 6) 6
25 include ltiostreamgtusing namespace std int
main () int Unus, Duo, Tres Unus Duo
Tres 5 Unus 4 Duo 2
Tres - 4 Unus / 3 Duo Tres
cout ltlt Unus ltlt endl cout ltlt Duo ltlt endl
cout ltlt Tres ltlt endl return 0
5
9
3
5
5
10
11
1
3 _
11 _
1 _
26 include ltiostreamgt int main ()using namespace
std int Unusual, Mystery, Strange cout
ltlt (Unusual 2) ltlt endl cout ltlt (Strange
Unusual 3) ltlt endl cout ltlt (Mystery
Strange 2) ltlt endl
2 _
5 _
10 _
10
2
5
27 cout ltlt Unusual ltlt endl cout ltlt
Strange ltlt endl cout ltlt Mystery ltlt
endl return 0
2 _
5 _
10 _
2 _
5 _
10
2
5
10 _