* to "self-test" on how you are doing with the
basics of mutation/local variables/assignment
statement:
int i;
i = 7;
i = i + 1;
...what is i after the third line? 8
...if this is clear to you, that's a good sign!
* another self-test:
int i;
int j;
i = 3;
j = i + 7;
i = i + 1;
// at this point, i is 4 and j is 10,
// and if you see that, that is ALSO a good sign
* yet-another-way to assign a value to
a local variable: interactive input
using cin
* the C++ iostream library provides the object cout
for output to standard out/to the screen;
it ALSO provides an object cin
cin <-- pronounced see-in
...for interactive input.
for input from standard input,
so the user can type something
at the keyboard, and your program
can read it in
* cout has the side-effect of
printing to standard output/to the screen,
cin has the side-effect of
reading from standard input/from the keyboard,
and trying to assign it to a variable
* SO -- when you use this cin object,
the program stops and waits for
the user to type something
(something to be entered into standard
input)
BASED on the type of the variable in the
input stream,
cin reads in what the user types,
tries to convert it to the type
of that local variable,
then tries to assign the result
to that local variable
* how CAN you put a variable into cin's input
stream?
ONE way: is with its >> operator
* example:
double next_avg;
cout << "Enter a numeric average, followed by enter: ";
cin >> next_avg;
* this prints
Enter a numeric average, followed by enter:
...to the screen, and WAITS until the user
types something (hopefully a number, since
next_avg is a double!!)
BECAUSE next_avg is a double,
cin accepts input from the user until
the user enters white space (a blank, a tab,
or a newline character) OR a
non-number character????,
THEN it tries to convert what was read
(those characters) into a double value,
and tries to assign it to next_avg
****************
WARNING!!!!!!!!!
****************
* do not put ANYTHING in cin's input stream
EXCEPT for local variables or stuff that can
be set or boolalpha --
do NOT put endl or literals or other expressions!!!!
****************
* PSEUDOCODE
* when you type the steps you want to do
in "natural-language-ish" form,
in the order you want to do them;
(if you HAD a really smart computer,
and it did what you said,
you'd get the results you want)
* you write the steps,
perhaps high-level steps first;
then, if necessary, you "break down"
a step into lower-level steps
until you feel like, hey, I can now
convert this into code;
* AFTER your examples,
in the design recipe,
if you aren't sure what to do next to
create the function body,
you COULD try to pseudocode HOW you got
from user-input to what-happened in
your examples;
hey, you could also draw a flowchart
if that works better for you!
* a few more cin tidbits:
you can put multiple variables IN a cin stream;
...how does it know when to "stop" reading 1 value
and "start" reading the next?
...it makes ASSUMPTIONS based on the TYPE of the
variable in the input stream!
* for int and double,
keeps reading appropriate-number-y characacters
until white space or a non-number-y character
is typed,
then tries to convert what was read to int or
double respectively,
and tries to assign it to the variable;
for string,
it keeps reading until you enter white space
(yes, this means you cannot enter a string
with a blank in it --
you need another means, hopefully to be
discussed next week...)
...and tries to convert it into a string,
and tries to assign it to the string variable;
for char,
I THINK it reads a single non-white-space character
...and tries to convert it into a char,
and tries to assign it to the char variable;
for bool,
IF you have added boolalpha to the cin input stream,
then it reads:
true
...as bool true, and
false
...as bool false
...and tries to convert it into a bool,
and tries to assign it to the bool variable;
* QUICK STYLE NOTE:
* CS 111 CLASS STYLE:
thou shalt NOT change (most) parameters!!
^ it is poor style, for reasons we will
discuss later!!
* INTRO OUR FIRST LOOP!!
* in the imperative style,
local variables can be used to control
repetition,
in a so-called LOOP statement.
* C++ has several of these,
here is the first:
while statement/while loop
while ( bool_expr )
{
statement1;
statement2;
...
statement_n;
}