Cin in cpp :
For taking the input from the user in program the cin is used .
By default keyboard is standard input in any programming language.
Cin is the instance of iostream.h class.
For Formatted input operation cin is used.
Cin is used together with the extraction operator, which is written as >> ( two greater than opeartor sign ). This Operator is followed by the variable where the extracted (input value ) data is stored.
cin >> ;
In syntax we see first we write cin function followed by extraction operator >> and variable name in which we are storing the input value. And end with semicolon.
int no; // Declare the variable with int type
cin >> no;
This statement takes the input integer number from user and store that value to the variable no.
As we have different data type in c++. For taking input for these different datatype their is no change in taking the input.
we can take any type of input using cin from user (int , float , char)
Multiple Extraction operator :- Extraction operator is chained to take multiple input at a same time from user.
cin >> a >> b ;
This will take the two input from user and store value in a and b variable.
This is equivalent to
cin >> a ;
cin >> b ;
User enters two different values for variable a and b by giving space , tab or enter .
Program for Input stream
using namespace std;
int no ;
cout << " cin Input Stream\n";
cout <<" Enter character :" ; // Taking character input
cin >> ch ;
cout << " Enter two numbers :"; //Taking two number
cin >> no1 >> no2;
cout << " Char is = " <<ch<<endl;
cout << " Two numbers = "<<no1<<","<<no2;
Program description :
1. In above program we see how to use cin function to take input from user.
2. First we declare variable of type int and character. int no1, no2 and char ch;
3. Next , we take the input character from user using cin>> ch;
4. Next, we take two input at a same time by extraction operator chaining as cin>>no1>>no2. Which takes two input from user.
5. Print value of no1 ,no2 and ch using the cout ( output stream).