Variables are names under which you can store and later invoke information. They come in many forms (programmers can create their own, in fact), but there are a sect of basic variable types known as primitives. Before I go there, though, it is important to note that variables (in their simplest form) can only store one type of information; if you want an integral number (with no decimal places), you have to make your variable an int. If you want a string, you have to make your variable of the char data type. Lastly, decimal numbers are doubles.
For now, we'll stick with these three simple, primitive data types for the simple fact that they are significantly easier to work with. So, you must first decide what type of data you want to work with; we'll start with an integer. Next, you need to give it a name, which must consist of nothing other than alphanumeric characters and underscores -- the first character of the name must be either a letter or underscore. Lastly, you have the option of giving it an initial value. When all is said and done, you'll end up with the following:
int myInteger = 0;
The word "int" is the space in which you'll specify your data type: int, char, double and the like. Next, myInteger is the name that we assigned our variable. Lastly, " = 0" is the optional initialization step. If you don't want it, you don't need it. You'll also note the semicolon after the line. You must place a semicolon at the end of each line of code unless it is noted otherwise.
I'll also mention a few side-notes to the whole variable declaration schpiel. First, you can slightly modify the type of data stored with a short list of modifiers that you place before your data type declarations. These are short, long, unsigned, and const. Short and long increase and decrease the capacity of the variable (they are generally used with number-holding primitives, such as int or double); unsigned, also used with numeric variables, forces the value to remain positive, and const renders the variable unchangeable (beyond initialization, that is).
The second side note is the fact that you can declare and/or initialize multiple variables of the same data type on the same line. Simply separate each one with a comma, and omit the data type and any modifiers in subsequent declarations. Note that it is considered of questionable convention to do this. The space saved is negligible compared to the potential for confusion. That said, here's a short splash of all that info:
long int my_var = 3, my_second_var = 5; double myDouble,mysecondDouble;
The first line declares two long ints, my_var and my_second_var, and initializes them to 3 and 5, respectively. Notice that the "long int" status persists through all of the declarations on the line (i.e., prior to the semicolon; C++ doesn't care about white space). Also shown are the two uninitialized doubles myDouble and mySecondDouble. Nothin' to it, eh?
Input and output (heretofore referred to as I/O) in C++ is a thankfully simple task in console applications. There may be other conduits out there, but the standard is iostream. To peruse it, add the line #include <iostream.h> to the beginning of the file. From there, you can print output to the screen via the "cout <<" structure, like so:
cout << "The value of variable x is " << x;
Notice that it, as stated, begins with the cout <<. Beyond that, note that each "individual" piece of information is delimited by another set of <<'s. "Individual" information is single strings, single variables, or variable/string combinations strung together with a legal arithmetic operation (more on those later!).
It is a fairly self-explanatory affair, but there is a little more to the story. Within strings, you can insert special characters (known as escape characters) that force some degree of formatting on your output. There are a number of them, but for the scope of this tutorial I'll only mention two: t and n. t simply places a tab at the spot it is placed within the string. Tabs are, in general, just 5 spaces. n inserts a line break. This is also doable with endl, which can be considered a variable which contains nothing but the n escape character (i.e., you place it in cout statements just like you would any other variable -- all alone between << tags). An example:
cout << "The value of x ist" << x << endl;
Any questions? Refer to the paragraph above for answers!
Input is largely the same. Simply use "cin >>" and then the name of the variable into which you want the user to place information. When the program reaches the statement, it will sit and wait until the user hits the "enter" key on their keyboard and attempt to place whatever they typed into the specified variable. Be warned, though, that trying to stuff a character into an int will not sit well with the program; it will spin wildly into an endless loop. Fairly amusing the first time, but frustrating thereafter, so be careful about it. (Yes, there are ways to prevent this which I'll try to discuss later). Anyways, here's an example of cin >> in use:
cin >> x; cin >> x >> y >> z;
The first line sits and waits, then stores the contents of the keyboard buffer into x (or rather, attempts to). The second line, however, stops and waits not once, but thrice; it stores the values from left to right just as you'd expect. One thing that I did not do here that you absolutely MUST do in your code is declare your variables before using them in either a cin or cout statement. Failure to do so will result in an unmaking of the universe. Or your program will crash. I always get the two mixed up.
For reference, here's a brief program detailing everything we've learned thus far:
#include <iostream.h>
#include <stdlib.h>
int main()
{
int x,y;
cout << "Enter the value of x and then y.n";
cin >> x >> y;
cout << "This is the value of x: " << x << endl;
cout << "y << " is the value of y.n";
system(?pause?);
return 0;
}