cpp / beginner
Snippet
User Input with cin
cin (console input) reads user input from the keyboard. The >> operator extracts values and stores them in variables. It automatically handles type conversion for numbers.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>#include <string>int main() {std::string username;int age;std::cout << "Enter your name: ";std::cin >> username;std::cout << "Enter your age: ";std::cin >> age;std::cout << "Hello " << username << ", you are " << age << std::endl;return 0;}
Breakdown
1
std::cin >> username;
Reads a word and stores it in the string variable
2
std::cin >> age;
Reads an integer and stores it in the int variable
3
std::cout << ... << username << ...
Outputs multiple values including the stored input