Lesson 5: Input with std::cin
In C you read input with scanf and &. In C++ you read with std::cin and the >> operator. Notice the arrows: cout uses << (out, to the screen), and cin uses >> (in, into the variable). In this lesson we'll learn one thing — read a value from the user into a variable. No & needed like in scanf.
cin is the opposite of cout: instead of sending to the screen with <<, it pulls from the keyboard into a variable with >>. The arrow always points toward the variable.
- std::cin
- C++'s standard input object. You read from it into a variable with >>, e.g. std::cin >> x;
- >> (extraction operator)
- Pulls a value from std::cin into the variable on its right. The direction is opposite to cout's <<.
- word-by-word reading
- std::cin >> reads one value up to the next whitespace/newline. A second word needs another >>.