cpp / beginner
Snippet
User Input with cin
The cin object reads input from the keyboard. Using >> (extraction operator), you can store user input into variables. The program pauses until the user types and presses Enter.
snippet.cpp
1
2
3
4
5
6
7
8
9
#include <iostream>#include <string>int main() {std::string name;std::cout << "Enter your name: ";std::cin >> name;std::cout << "Hello, " << name << "!" << std::endl;return 0;}
Breakdown
1
#include <string>
Header needed for std::string type
2
std::string name;
Declare a string variable to store the name
3
std::cin >> name;
Read user input and store it in name variable
4
std::cout << "Hello, " << name << "!";
Output greeting with the entered name