capypad
0 day streak
cpp / beginner
Snippet

Reading User Input with cin

The cin object reads input from the keyboard. The >> operator extracts value from cin and stores it in the variable on the right. This allows interactive programs that respond to user input.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
 
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You entered: " << age << endl;
return 0;
}
Breakdown
1
cin >> age;
Waits for the user to type a number and stores it in the age variable
2
cout << "Enter your age: ";
Prompts the user with a message before expecting input
3
cin >>
The extraction operator - takes data from cin