Title: Input/Output
1Input/Output
Disk Drive
istream
Disk Drive
ostream
Main Memory
Monitor
Keyboard
Printer
stream sequence of bytes
Scanner
2Input/Output standard library
- Unformatted stream of bytes. Good for high
speed, high volume transfers - Formatted groupings of bytes. Integers,
characters, etc.
3iostream library - cin, cout, cerr
- an istream object named cin associated with the
keyboard - an ostream object named cout associated with the
monitor - an ostream object named cerr associated with the
monitor
4operators
- ltlt stream insertion operator
- int grade 5
- cout ltlt grade
- gtgt stream extraction operator
- int grade
- cin gtgt grade
5cout details
int grade 92 cout ltlt grade // causes 92 to
be displayed on the screen
integer
char
92
grade
2
9
4 bytes
2 bytes
- steps
- convert integer 92 to character sequence 92
- send characters 9 2 to the screen
6cout details
cout ltlt 9 ltlt 2 // causes 92 to be displayed
on the screen
char
char
2
2
9
9
2 bytes
2 bytes
no conversion necessary cout knows what to do
because it knows the data type
7concatenation of stream operators
int grade 92 cout ltlt your grade is ltlt
grade is the same as cout ltlt your grade
is cout ltlt grade
8cout escape sequences
- \ escape character. special character follows
- cout ltlt Hello \n
- \n newline
- \\ backslash
- \ double quote
- \t tab
9stream manipulators
- endl writes a newline, flushes buffer
- cout ltlt your grade is ltlt grade ltlt endl
- whats a buffer?
- place to hold data while waiting for I/O
operation to complete.
i/o device takes things out
cout puts things in
2
9
first in first out buffer
1 second 1000 milliseconds 1
million microseconds cout ltlt grade //
microseconds to execute
// milliseconds or seconds to complete I/O
10stream manipulators
- double veryPreciseNumber 1.123456789
- cout ltlt veryPreciseNumber ltlt endl
- prints 1.12346
- cout ltlt setprecision(10) ltlt veryPreciseNumber
- prints 1.123456789
- include ltiomanipgt //needed to use
setprecision - stays in effect for all subsequent cout calls
- setprecision(0) sets it back to default value
11stream manipulators
- cout.setf(iosshowpoint)
- ioshex Format numeric values as base 16
(hexadecimal). - iosshowpoint Show decimal point and trailing
zeros for floating-point values. - iosshowpos Show plus signs () for positive
values. - iosscientific Display floating-point numbers
in scientific format. - iosfixed Display floating-point numbers in
fixed format.
12cout Exercises
Identify the error in each of the following
statements and explain how to correct it. 1)
The following statement will print the integer
value for c. cout ltlt c 2) cout ltlt A
string in quotes ltlt endl 3) display the number
200 with and without the sign.
13cin
- int age
- cin gtgt age
- operand to right of gtgt must be a variable
- nothing read until Enter key is typed
- reads input until end of input value is reached.
- 12 (age 12)
- 1 2 (age 1)
- input data must match data type.
- x would be an error for input for age.
14cin
- concatenation okay
- int age, weight
- cin gtgt age gtgt weight
- Input 25 150
- cin statement causes program to wait until input
is entered. Should precede it with prompt - cout ltlt Please enter age and weight
- handout on cin in Prof Hamels section of web
site - C How to program, Harvey Deitel Chapter 11
15cin.get
Useful when you want to be read whitespace
characters i.e write a program to change a single
spaced document into a double spaced document.
char nextCharacter cin.get (nextCharacter)
16cin Exercises
For each question, assume the following
declarations int num1, num2, num3
double real1, real2, real3 Each question is
separate. 1) cin gtgt num1 gtgt num2 gtgt num3
INPUT 11 22
33 44 2)
cin gtgt real1 gtgt real2 gtgt real3 INPUT 1.1 2
3.3 4 3) cin gtgt num1 gtgt num2 gtgt num3
INPUT 1.1 2 3.3 4 4) cin gtgt num1 gtgt real1 gtgt
num2 gtgt real2 gtgt num3 gtgt real3
INPUT 1.1 2
3.3 4
5.5 6