We can use cin to get input from user. Input will be stored to a variable. So we need a variable. We can use cin for variable with any types.
#include <iostream>
using namespace std;
int main(){
int n;
cout << "Number : ";
cin >> n;
cout << "Your number is " << n;
return 0;
}
Example :
Number : 23 Your number is 23
There are a few other functions that we can use for getting input from a user.
Getline
Cin will stop to take characters after space. It will be usefull to get numbers from user input. But, if you want all characters in a line, you will need other functions or methods.
#include <iostream>
using namespace std;
int main(){
string n;
cout << "Name : ";
cin >> n;
cout << "Your name is " << n;
return 0;
}
Example :
Name : Gerimisya Stela Rida Your name is Gerimisya
If you want to take a line of characters, you need getline.
#include <iostream>
using namespace std;
int main(){
string n;
cout << "Name : ";
getline(cin, n);
cout << "Your name is " << n;
return 0;
}
Example :
Name : Gerimisya Stela Rida Your name is Gerimisya Stela Rida