Please send questions to
st10@humboldt.edu .
C++ has a datatype: char
* 'f'
* a single quote, a character, and a single quote
* '\t' - a tab character
'\n' - a newline character
'\\' - a backslash character
char answer;
cout << "enter an initial: ";
cin >> answer;
char initial = 'T';
initial = 'T';
'2' "2" 2.0 2
* a ONE-DIMENSIONAL array...
* has ONE name
* in C++, all must be of the same TYPE
* and I can access individual elements within by INDEX (or by
POSITION)
...where the FIRST thing has index ZERO (0)
* to declare an array named stuff with NUM_ELEMS things in it
of type a_type,
a_type stuff[NUM_ELEMS];
const int NUM_STUDENTS = 30;
int grades[NUM_STUDENTS];
const int NUM_GERBILS = 45;
double gerb_wts[NUM_GERBILS];
* what if I want to fill the gerb_wts array interactively?
const int NUM_GERBILS = 45;
double gerb_wts[NUM_GERBILS];
int i = 0;
double next_wt;
while (i < NUM_GERBILS)
{
cout << "enter next gerbil weight: ";
cin >> next_wt;
gerb_wts[i] = next_wt;
i = i + 1; // i++;
}
i = 0;
while (i < NUM_GERBILS)
{
cout << gerb_wts[i] << endl;
i = i+1;
}
i = 0;
while (i < NUM_GERBILS)
{
gerb_wts[i] = gerb_wts[i] * 1.10;
i = i + 1;
}
* a FOR loop is a convenience to make a count-controlled loop
more bullet-proof....
for (init_sttmt ; bool_expr ; update_sttmt )
{
sttmt;
}
for (i=0 ; i < NUM_GERBILS ; i = i+1)
{
gerb_wts[i] = gerb_wts[i] * 1.10;
}
for (int i=0 ; i < NUM_GERBILS ; i++ )
{
gerb_wts[i] = gerb_wts[i] * 1.10;
}
* simplest strings in C++ are arrays of type char...
const int MAX_NAME_LENGTH = 15;
char last_name[MAX_NAME_LENGTH];
* C++ (ANSI standard) has a standard string class library;
#include <string>
using namespace std;
.... string first_name;
* ...but cin works better with arrays-of-char strings:
cout << "type in a name: ";
cin >> last_name;