4. Input and Output Flashcards
I/O
sequence of bytes (stream of bytes) from source to destination
stream
sequence of characters from source to destination
input stream
sequence of characters from input device to computer
output stream
sequence of characters from computer to output device
standard I/O devices
use iostream to extract data from keyboard + send output to screen
iostream contains definitions of 2 types:
istream - input stream
ostream - output stream
iostream’s 2 variables
cin - stands for common input
cout - stands for common output
using cin and cout
-include iostream must be used (preprocessor directive)
-declaration is similar to these statements:
istream cin;
ostream cout;
extraction operator»_space;
left hand operand
an input stream variable such as cin
extraction operator»_space;
right hand operand
a variable of simple data type
extraction operator»_space;
syntax using it with cin
cin»_space; variable»_space; variable …;
use of»_space;
every occurence of»_space; extracts the next data item from the input stream
eg. cin»_space; x»_space; y; is equivalent to cin»_space; x; cin»_space; y;
skips all the whitespace
whitespace characters
consist of blanks and certain nonprintable characters
data type of input
eg.»_space; distinguishes between character 2 and number 2 by the right hand operand of»_space;
(if it is char, 2 is treated as character 2, if it is int, it is treated as number 2)
reading data: reading into char variable
- eo»_space; skips leading whitespace, finds + stores only next character
- reading stops after a single character
reading data: reading into int/double variable
- skips leading whitespace, reads + or - (if any), reads digits (incl decimal)
- stops on whitespace non-digit character
cin»_space; z
z is adouble and 39 is input. What is the value of z?
z = 39.0
functions
- (subprogram)
- set of instructions
- when activated, it accomplishes a task
main function
executes when a program is run
other functions only execute when called
header files
predefined functions organised as a collection of libraries called header files
a header file can contain several functions
predefined functions
to use, need name of appropriate header file