Account Login
 

Introduction to Variables and iostream
 
Introduction:
In this tutorial you will be introduced to variables. You will learn how to store user input in these variables do alterations the the data then display the results on the screen.
 
 
Lets Get Started:
1. Go ahead and start a new console project. If you are unsure how to do this go back and follow the previous tutorial. (your first c++ program)
2. Type in the following code in the window.



#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
  string FirstName;
  string LastName;
  int BirthYear;
  int BirthMonth;
  int BirthDay;
  cout<<"Please Enter The Following Information \nFirst Name:";
  cin>>FirstName;
  cout<<"Last Name:";
  cin>>LastName;
  cout<<"Birth Year:";
  cin>>BirthYear;
  cout<<"Birth Month:";
  cin>>BirthMonth;
  cout<<"Birth Day:";
  cin>>BirthDay;

  cout<<"\nWelcome "<<FirstName<<" "<<LastName<<endl;
  cout<<"You Birthday is "<<BirthMonth<<"/"<<BirthDay<<"/"<<BirthYear<<endl;
  system("pause");
  return 0;
}
 
 
3. Go ahead and compile and run your program. It will ask for your first and last name and your birth day.
4. Now try and run it again. This time instead of typing in a numeric month type the name of the month. Your program flipped out didn't it. The reason why is because of how we setup the variables.
5. Ok so lets start at the top. the first change from the first program you will see is another #include statement. This time we are using the string class. Basically a string is a set of characters with an undetermined length. In C you would have to setup an array of characters to store any kind of words. The problem with this is that it was a set length. Now using the string variable type you don't have to worry about this. For the purest many still use a standard character array because of the speed enhancements.
6. Ok now just inside the main function we have our variable declarations. The way you declare a variable is to type in its data type followed by a unique name that can be used to access that information later. You will see that our first variable is a string called FirstName. One important note here is that variables can not start with a number and that they are case sensitive so FirstName, firstname, firstName are all different. After our first and last name variables you will see we have an int for our birth month, day and year. Wait! what an int? Well an int is short for integer. I guess now would be a good time to list some of the common data types and what kind of information they can hold.
7. Standard Variable Types - ***Note*** Size and range are different of some compilers. This however is the standard for 32bit systems. This is not a full list either but does have most of your common types.
Name Description Size Range
char Character or small integer. 1byte signed: -128 to 127
unsigned: 0 to 255
short Short Integer. 2bytes signed: -32768 to 32767
unsigned: 0 to 65535
int Integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long Long integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
bool Boolean value. true or false. 1byte true or false
float Floating point number. 4bytes 3.4e +/- 38 (7 digits)
double Double precision floating point number. 8bytes 1.7e +/- 308 (15 digits)
 
8. Ok so lets go back to our code. You should already familiar with the cout function, if your not go back to the previous tutorial. One new thing you will see in the cout line is a \n. This just tells it to start a new line. Now lets move to the cin function. Notice that it looks very similar to the cout function. The main difference is the direction of the >>. Basically for the cin function to work place the variable name that you want to use to store the keyboard input to the right of the >>. So what we do here is ask for name and birthday information then store each of the inputs into separate variables.
9. Last we use the cout function to display the values stored in our variables. That's it we have setup variables to hold our information, used the cin function to store keyboard input and finally used cout to display the data stored in the variables.
 
Thought you were done? Not Yet!
While the tutorial is officially over we still have some things about variables to discuss. In the above tutorial we only stored and displayed the information. There is much more you can do with a variable. You can make calculations, update the information stored in them and you can also set their starting value when you declare them.
Lets see a little example



int a = 5;
int b = 10;
int c = 0;
c = a+b;
 
in this example we declared a and initialized it to the value of 5. Then we do the same for variables b and c. Last we store the value of a + b in c. So in this instance c = 15.
 
Conclusion:
Ok so you have had a very simple introduction to variables. This tutorial is by no means a complete guide to variables, but like the title just an intro. You have also been introduced to a new class "string" which will extend what you can do with text data.




Home | Tutorials | Articles | Free Software | Advertise With Us | Contact Us
© 2008 All About Coding