cin (pronounced "see-in") reads input from the keyboard. It uses the extraction operator>>.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You are " << age << " years old." << endl;
return 0;
}
Sample Run:
Enter your age: 25
You are 25 years old.
getline — Reading a Full Line
cin >> stops reading at the first whitespace. To read a whole line including spaces, use getline():
#include <iostream>
#include <string>
using namespace std;
int main() {
string fullName;
cout << "Enter your full name: ";
getline(cin, fullName);
cout << "Hello, " << fullName << "!" << endl;
return 0;
}
Sample Run:
Enter your full name: John Smith
Hello, John Smith!