Reading what somebody types
std::cin, somewhere to put what it reads, and the one thing about it that catches absolutely everybody.
A program that says the same thing every time it runs is a sign, not a program. What makes one useful is that something goes in, and the simplest something is whatever the person at the keyboard types.
Reading a value needs two things: an instruction that reads, and somewhere to put what was read. C++ will not simply hand you the answer, because it has to be told in advance what shape of thing to expect.
Somewhere to put it
A variable is a named place that holds one value. You make one by writing what kind of thing it holds, then the name you want for it. The whole of the next section is about the kinds; two of them are enough here. int holds a whole number, and std::string holds a piece of text.
int age = 30;
std::string name = "Ada";You can also write int age; with no starting value, which is what you do when the value is about to arrive from somewhere else. std::string needs #include <string> at the top of the file, in the same way std::cout needs #include <iostream>.
std::cin and the other arrow
std::cin is the standard input, the counterpart to std::cout, and it comes from the same header. That is the io in iostream: input and output. Where << sends a value out, >> brings one in, and both arrows point the way the value travels. std::cin >> age moves a value from the input into age.
There is no special instruction for asking a question. You print the question with std::cout, then read the answer with std::cin. The question is called a prompt, and it is nothing more than a line of output that happens to end without a new line so the answer appears next to it.
Change Ada in the input box to another name and run it again. The input box stands in for someone typing, and nothing typed is echoed back, so the prompt and the greeting end up next to each other on one line.
The arrow reads one word, not one line
This is the part that catches everybody once. >> skips over any spaces and newlines in front of the value, then reads until the next space or newline, and stops there. Given Ada Lovelace, it reads Ada and leaves Lovelace sitting in the input, untouched, waiting for whoever reads next.
Put the two names on separate lines in the input box and run it again. >> treats a newline exactly as it treats a space, so it makes no difference whatsoever.
Reading a whole line
When the answer can contain spaces, an address or a film title or a full name, >> is the wrong tool entirely. std::getline reads everything up to the end of the line, spaces included, puts it in the variable you name, and throws the newline away.
Change the title in the input box to one with a comma and an apostrophe in it, keeping the rating on the second line, then run it again. The whole first line arrives exactly as typed, spaces and punctuation included.
The name comes back empty, which is why the square brackets are there: without them you would not be able to see it. Change the getline line to std::getline(std::cin >> std::ws, name); and run it again.
Reading numbers
Reading into an int works the same way, except that >> now reads digits and stops at the first character that cannot be part of a whole number. If the very first character cannot start a number, nothing is read, the variable is set to 0, and the input goes into a failed state in which every later read does nothing at all.
Noticing that failure and recovering from it needs tools from a later section. For now it is enough to know the symptom: a program that suddenly reports zeros, or asks a question and does not wait for the answer, has usually been given something it could not read.
Change 4 in the input box to banana and run it again. Nothing crashes, and that is exactly the trap: the total is simply wrong, and quietly so.
Making one piece of text out of several
<< sends pieces to the screen one after another and never builds anything out of them. Sometimes you need a single piece of text made of several parts instead, particularly when your code has to hand an answer back rather than print it. + joins two strings end to end, and std::to_string turns a number into the characters that spell it.
Change 250 to another number and run it again. Then change " costs " to " now costs ". Every piece being joined has to be text already, which is the whole reason the number goes through std::to_string first.
A form asks for a house number and then for the street. It reads the number with >> and the street with std::getline, and the street always comes back empty. What happened?
The >> read the digits and stopped, leaving the newline at the end of that line in the input. std::getline then read from there, met the newline straight away, and returned the empty text in front of it. Writing std::getline(std::cin >> std::ws, street) skips the leftover whitespace first and fixes it.