switch (variable)
{ case firstvalue : dothis;
dothat;
somethingelse;
break;
case secondvalue : anotheraction;
andanother;
break;
etc.
}
variable is the variable to be tested and firstvalue, secondvalue etc. are constants (not variables, but values) representing the possible values that the variable can take.
int myname[30];
myname[2] = 34; myname[15] = 4 * myname[7] - 10; cout << myname[20];
// Display all the values from myname[0] to myname[9] for (int x = 0; x < 10; x++) cout << myname[x] << " ";
double airpressures[40][25];
...
airpressures[3][index] = 45.11;
...
for (int x = 0; x < 40; x++)
airepressures[x][0] *= 3;
...
for (int a = 0; a < 40; a++)
for (int b = 0; b < 25; b++)
airpressures[a][b] = 0;
The same applies to three-dimensional arrays etc. except they have three index numbers.
char name[20]; cout << "Enter the name : "; cin >> name; cout << "The name is " << name << endl;
You will notice that there is no messing around with array indices in that. The last character of the string is an invisible character of value 0.
string name, address, girlfriend;
girlfriend = "Diane"; cout << girlfriend[0]; // Displays D
Remember, the first character of the string is still in slot 0, not 1.
cout << "Enter your full name : "; cin.getline(name,20);
In this case, the second parameter (the number after the name of the string variable whose value is to be read in) is the maximum number of characters that you want getline() to accept. If you try to enter more than this number of characters, getline() simply ignores the rest.
return_type function_name (parameter_list)
{
// The statements making up the function go here
}
The return type is a simple variable type (int or double etc. but not something complicated such as array). This means that the function can be called (referred to by name) in place of a variable, just as if it were a variable itself. If you want to refer to the function without it handing back a value (i.e. just as sequence of instructions), then use the special word void for the return type.
// This function gives 'x' to the power of 'y'
long power (int x, int y)
{ long temp = 1;
int i;
for (i = 0; i < y; i++) // Multiply temp by 'x', 'y' times
temp *= x;
return temp;
}
...
cout << "The value of 17 squared is ";
answer = power(17,2);
cout << answer;
In this example, the function was called power and returned a long value. It had two parameters, both int values, which were referred to as x and y inside the function. The function calculated the value of x to the power of y in a variable called temp which it returned at the end.
The function was called by quoting its name and passing it two parameters, in this case, the numbers 17 and 2. The parameter x became 17 for the duration of this function call, and y became 2 for this function call, although they could be totally different if the function were called again with different values. The function was called as part of a variable assignment, just as if it were a variable or a number.
double* first; int *x;
Pointers have to point to a certain type of item. In this example, the first pointer can only point to a double value and the pointer x can only point to an integer. The asterisk can come at the end of the type or at the start of the variable name.
int hello = 4; x = &hello;
This instruction means "make 'x' point to the variable 'hello'".
hello = -62; *x = -62;
The instruction *x means "the variable that x refers to".
int goodbye = 107; x = &goodbye; *x = -62; cout << goodbye << endl; // Now has the value -62.
in this case, the same instruction *x = -62 changes the value of the variable goodbye, not hello.
struct my_structure_name
{
int first;
double heresanothervariable;
string yetanother;
float anarrayaswell[56];
};
The structure type is called by the name you give it (after the word struct). The variables inside the structure (called member variables) are declared just like any other variables, but within curly brackets. Don't forget the semicolon after the closing curly bracket (I always do!)
struct dog
{ string name;
int year_of_birth;
double height;
};
dog pet, guarddog, stray; // Variables of type dog
pet.name = "Dylan"; guarddog.year_of_birth = 1990;
dog dalmatians[101]; dalmatians[0].name = "Pongo"; dalmatians[0].height = 75.5; // 99 of the dogs were born in the same year for (int i = 2; i < 101; i++) dalmatians[i].year_of_birth = 1996;
void do_something (dog d)
{ cout << d.name << endl; // Refer to dog as 'd' here
}
#include <fstream.h> const char* fname = "mydata"; ifstream f(fname);
Notice that the file access commands are included in a header file which you must include. The name "mydata" is the file name on the disc (which can include full path names, extensions - the full works), and the instruction ifstream opens the file as an "input stream". And here is a file being opened for output (an "output stream"):
const char* file2 = "c:\\MI5\\secrets.txt"; ofstream filevar(file2);
Note that path names must be specified using a double \\ character rather than a single one, as this is how you include a \ character in a string (A single one is used by the compiler to recognise control codes such as \n for a new line.
// This writes out to the file 'f' on the disc f << "The variable has the value " << x << endl; // This reads a variable from the file 'filevar' on the disc filevar >> temp_var;
// Read a line from file 'filevar' to // string variable 'anothervariable' filevar.getline(anothervariable);
filevar.close(); f.close();