CS 112 - Week 3 lecture 1 - 2022-09-06
TODAY WE WILL
* announcements
* more C++ overview/review!
* interactive input in C++
* (if time) FILE input/output in C++
* prep for next class
* if desired:
* see Savitch Ch. 6 - more on interactive
i/o and file i/o
* see Savitch Ch. 7 - review of arrays
=====
review: interactive output
=====
* C++ iostream library provides classes and functions
and some predefined objects for interactive stream
input/output
* remember: printing to the screen
is NOT the same as RETURNING a value!
* return: ENDS a function then and there,
and its expression becomes the value of
the completed function call
* something like cout with << operator
has the SIDE-EFFECT of printing to
standard output -- does not end the function,
what is written is not the value of the
current function call
* cout is an output stream object
defined in iostream library
and set to output to standard out
* VERY frequently used with the operator <<
(sometimes called the insertion operator)
IN THIS CONTEXT
and after the << is expected to be an expression
whose value is to be written to the output
stream on the left-hand-side of the <<
using the default format for that expression's
data type
* char data is depicted without single quotes
* string and and char* data are depicted
without double quotes
* double data - only about the first 7
significant decimal places are shown
* and if a double value has no
fractional (.0), then it is displayed
with no fractional part (and no decimal
point), even though it is stored as a
double within the computer
* these default depictions can be modified!
...for example, putting the special object
boolalpha in an output stream requests
that bool expressions be depicted as
true, false
* (and there are means for right-justifying
string and char* data, depicting double
values to a specific number of decimal
places, etc.)
* and the object endl or "\n" or '\n'
says you want to output a newline character
=====
* iostream also provides objects and classes and
functions for interactive input!
* cin is a predefined object attached to
standard input, typically the user's
keyboard
cin can be used with the >>
(sometimes called the extraction operator
IN THIS CONTEXT) followed by an lvalue
(an expression, such as a local variable,
that can be assigned to,
that can be on the LEFT-hand-side of
an assignment statement),
and it reads from the standard input/keyboard
based on the lvalue's type,
and tries to convert the characters entered
into that lvalue's type and assign to it
* NOTE that
cin >> a_var;
does cause the program to stop and wait
for input from the user
so we usually proceed this with a cout
giving a PROMPT asking the user to enter
something
* BUT there are also functions that expect an
INPUT stream as an argument,
and cin can generally be used in those
* some general cin-with->> input rules:
* lvalue of type int?
waits for (optionally) a + or -
then (definitely) one or digits
and the first non-digit STOPS it
* lvalue of type char?
waits for first non-blank-or-tab-or-newline
character
* lvalue of type string?
waits for the first non-blank-or-tab-or-newline
and then reads in all the
non-blank-or-tab-or-newline characters until
a blank-or-tab-or-newline is entered
* lvalue of type double?
...like an int, but a decimal point is OK
* what if you WANT blanks in your string?
tabs? what if you WANT the option of an empty
string entered?
You want the getline FUNCTION!
getline's 2-argument version...
(getline is overloaded! it has a 2-argument
AND a 3-argument version)
...expects an input stream and a string lvalue
and it reads every character entered from
then until a newline is entered into that
string lvalue