Please send questions to
st10@humboldt.edu .
* now adding in cin
* cin - the object defined in iostream
that supports interactive(console)
input
* cin reads from the keyboard (typically),
tries to convert what it reads to the
type of the variable-or-other-lvalue
and then assign it to it
* (it is working in an input stream...)
* you use >> typically as the operator with
cin (opposite of what you use with cout --
it IS an input stream rather than an output
stream, after all...)
* a typical simple usage:
double next_avg;
cout << "Enter the next average: ";
cin >> next_avg;
* let's try it: see example
inter_greet.cpp
* also see: cin_play.cpp
* now: I'd like a function to let the user
interactively enter the values for an array...
// signature: get_nums: double[] int -> void
* WHAT?!?@? yes, this IS okay,
BECAUSE an array, to C++, is the address
of its first element -- THAT's what is
copied into the array parameter!
...so changing the contents of the parameter
array DOES change the contents of the
corresponding argument array!!
(if you change WHERE the parameter array
STARTS, that doesn't change where the
argument array starts...)
...so I can write a function to fill an array...
// purpose: expects an array of numbers and its
size, and it
produces nothing, BUT it has the
following side-effects:
* it asks the user for
the same number of values as
the array's size, and
* it CHANGES the argument array
to contain those values
void get_nums(double values[], int size)
example:
const int NUM_SCORES = 3;
double scores[NUM_SCORES];
for the call:
get_nums(scores, NUM_SCORES);
...the user will be prompted to enter 3
numbers; if he/she types
98
100
93.3
...when prompted, then after this call,
scores will contain {98, 100, 93.3}
pseudocode:
repeat size times
ask the user for a value
set the next value in values to that value
void get_nums(double values[], int size)
{
for (int i=0; i<size; i++)
{
cout << "enter the next value: ";
cin >> values[i];
}
}