User input: To enable the user to to input a value,use cin in combination with the extraction operator (>>)
. The variable containing the extracted data follows the operator.
The following example shows how to accept user input and store it in the num
variables.
For example:
int num;
cin>>num;
**Note:-**As with cout,extractions on cin can be chained to request more than one input in a statement:
int a,b;
cin>>a>>b;
Accepting user input: The following program prompts the user to input a number and stores it in the variable a:
For example:
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter a number\n";
cin>>a;
return 0;
}
When the program runs, it displays the massage "Enter a number", and then wait for the user to enter the number and press Enter
, or Return.
The entered number is stored in the variable a.