Free Hosting

A first look at variables (and cin)


In C++ we are required something in which we can store our data.For this we have facility of variables.Like if you want to write a program which can add two numbers first of all you have to store the two numbers in the memory of computer,but you can not keep track of memory locations so for this solution i C++ we have the facility of variables which have names to the memory and you can use these as following:-

  1. Suppose you want to handle integer numbers for this you can have :-
                   int number=100;

           In above number  is the variable which is holding 100 in the memory(RAM),the int is a data type which tells that it is of integer type.
            As above we can have as many variable as we want like:-
#include<iostream.h>
int main()
{
                    int number1=100;
                    int number2=102;
                    int sum;
                    sum=number1+number2;
                    cout<<"Sum="<<sum;
                    return 0;
}

     Now the problem is that if we want that we can can enter the numbers of which we want sum.Like if I will enter 500 and 400 the program will automatically sum them and print the result .
   So now the role of cin>>  come in the program ,lets try it:-

#include<iostream.h>
int main()
{
                    int number1;
                    int number2;
                    int sum;
                    cin>>number1;
                    cin>>number2;
                    sum=number1+number2;
                    cout<<"Sum="<<sum;
                    return 0;
}


So cin>> is used to get the input in the program and to store it in the variable.

0 comments:

Blogger Template by Clairvo