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
what you need to know to use predefined functions
function name no. of parameters req type of each parameter what function is going to do
predefined function example
-compute x using pow (power) function
-pow is in cmath library
pow takes 2 numeric parameters
-then returns a number
cin vs get function
> > cant read blanks or notice new lines and skips blanks + new lines
get function
- inputs next character (incl whitespace)
- stores character at location indicated by its arguments
sytax of cin and get function
cin.get(varChar);
eg.
cin.get(ch1);
cin.get(ch2);
cin»_space; num;
If I type “A 34”
ch1 = “A”
ch2 = “ “
num = 34
other istream functions
cin.ignore(intExp, chExp);
istreamVar.clear();
istreamVar.putback(ch);
istreamVar.peek();
cin.ignore example: cin.ignore(100, ‘.’);
-ignore next 100 characters or ignore input until it encounters ‘.’. Whichever comes first. The ‘.’ will be discarded too.
input failure
- input data does not match corresponding variables
- input stream enters fail state
- all further I/O statement using that stream ignored
- program continues to execute w/ values stored in variables
- causes incorrect results
syntax of cout and <
cout «_space;expression or manipulator «_space;expression or manipulator «_space;…;
called output statement
«_space;oeprator
called insertion operator or stream insertion operator
manipulator
alters output
endl
simplest manipulator, causes cursor to move to beginning of next line
\n
newline
cursor moves to beginning of next line
\t
tab
moves to next tab stop
\b
backspace
moves 1 space to left
\r
return
moves to beginning of current line
\
backslash
blackslash is printed
'
single quotation
single quotation mark printed
"
double quotation
double quotation mark printed
formatting output commands
setprecision(n)
fixed
showpoint
setw
setprecision(n)
outputs decimal numbers up to n decimal places
fixed
outputs floating-point numbers in a fixed decimal format
showpoint
forces output to show decimal point + trailing zeros
eg of setprecision and fixed
cout «_space;setprecision(5) «_space;f;
= 3.1416
cout «_space;fixed «_space;setprecision(5) «_space;f;
3.14159
setw
outputs value of an expression in specific columns
types of manipulators
with parameters and without parameters
parameterised
req iomanip header
eg. setprecision, setw, setfill
nonparameterised
req iostream header
eg. endl, fixed, showpoint, left, flush
I/O and string type
eo»_space; skips leading whitespace chars and reading stops at a whitespace character
reading strings with blanks
use function getline
getline
reads until end of current line
> >
extracts data from an input stream into a variable
«
pushes data into an output stream