In this tutorial, we'll add a little more functionality. We're going to create a program that gets a user's age, and prints it back out to them. In this progarm, you'll notice several new ideas. The frist thing that should strand out is that our main( ) function now looks like int main( ). This will be discussed after the program has been developed.

We know that to output data, we use cout and >>. Well, cout's brother, cin, use >>, and by default gets input from the keyboard. cin is defined in <iostream.h> as well. NOTE: cout and cin are pronounced as "see-out" and "see-in" respectively. cin is used in the form: cin << variable;

The astute rader may be wondering what variable means. A variable is simly area of memory that holds a value. This idea is very similar to basic algebra. For example: 2 + X = 5, so X = 3. X is a name used in place of the value 3. Before you can use a variable, you must declare it. You declare a variable like this: data_type Variable; The data type we will be using in this program is int.

int, as you may have guessed, is used to store integers. It can only store whole numbers, and cuts off any decimal places sent to it. So, if you declared nVariable = 3.4 (which, by the way, is how you define a variable's value), nVariable would only store 3 as its value. Setting a variable's value in this manner is called an assignment expression, where as '=' is the assignment operator. There are other ways to store a value into a variable, and one such way is with the cin << function. cin << variable will take whatever the user inputs up until the RETURN is pressed and set the variable with this value. NOTE: An int can only store values between -32768 and 32676. There is a way to make it hold larger values, but we won't get into this yet.

So far, this is what we have covered. The following code should makes some sense now:

int nMyAge;
cout << "What is your age?";
cin >> nMyAge;

As stated earlier, cout << can display "data." Data is not limited to static text; it can also be a variable. So cout << nMyAge; would display the value of nMyAge. To combine different pieces of data on the same line, repeat the "<<". For example: cout << "Your age is: " << nMyAge;. To move to a new line, do cout << endl;. This can also be used in conjunction with other data.

The only other topic to discuss before we have some code that works is our new main( ) function. int main( ) means main( ) returns an int value. This will be discussed in further detail in a future function tutorial. For now, just remember that's what it means. To return a value, use the return keyword (creative, eh?). You may be wondering why we would want to return and integer value. There is a very good answer for this. If the main( ) function succeeds, you should send it a return value of zero to let it know so. If it doesn't get this return value for some reason, then main( ) knows that it has failed, and the program will stop execution. Well, we now have enough information for our simple program. You should have a pretty good idea of what it does before you compile and run it.

#include <iostream.h>

int main( )
{
int nMyAge; // Create a variable of type integer
cout << "What is your age?" << endl; // Prompt user for their age
cin >> nMyAge; // Get the value from the keyboard
cout << "Your age is: " << nMyAge; // Display the age

return(0); // Return success
}

Try changing the code around a little. You now have the knowledge to play with variables a little bit. We'll have real fun with them in the future.

The source files for this lesson can be downloaded here.