capypad
0 day streak
cpp / beginner
Snippet

User Input with cin

The cin object reads input from the keyboard. The >> operator extracts data and stores it in the variable. Input is buffered until the user presses Enter. Always declare variables before reading input into them.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
 
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You are " << age << " years old." << endl;
return 0;
}
Breakdown
1
int age;
Variable declaration before reading input
2
cin >> age;
Extracts integer from input stream and stores in age
3
cout << "Enter your age: ";
Prompt user before reading input