Streams are the whole interface
Everything in this course communicates through standard input and output. Each example shows the text fed to the program on standard input and exactly what it prints to standard output, so fluency with cout and cin is not a warm-up. It is the interface every program in the course is read through.
Chaining output
Lesson 1-1 printed a single string. The << operator can be chained to print several values of different types in one statement:
int age = 24; std::cout << "Age: " << age << " years\n"; // prints: Age: 24 years
Chaining works because << returns the stream it just wrote to. So std::cout << "Age: " prints the string and hands back std::cout, and that returned stream is what << age writes into next. That is the actual mechanism behind values coming out strictly left to right, and it is worth knowing because the same trick explains cin later in this lesson.
Numbers are converted to text automatically, so straightforward output needs no equivalent of Python's f-strings.
You will also see std::endl. It prints a newline like \n and additionally flushes the stream, forcing the text out immediately rather than letting it sit in a buffer. Prefer \n in normal code, because it is faster, and save std::endl for the rare case where output must appear right away.
Comments
// single-line comment /* multi-line comment */
Mixing strings and numbers in one statement
Two chained statements, each combining fixed text with a variable of a different type. Neither one needs a format specifier or a conversion call.
#include <iostream> int main() { int problems = 150; double hours = 2.5; std::cout << "Solved " << problems << " problems\n"; std::cout << "Practiced " << hours << " hours today\n"; return 0; }
Output
Solved 150 problems Practiced 2.5 hours today
The stream picks the right conversion from each value's type, printing 150 as an integer and 2.5 with its decimal point. That is the payoff of static typing showing up early: because the compiler already knows hours is a double, the print statement does not have to be told.
Note the spaces inside the string literals, as in "Solved " and " problems". Streams write exactly the characters they are handed, so every space in the output has to appear in one of the strings.
Reading input with cin
std::cin is the standard input stream, and >> extracts values from it:
int x; std::cin >> x; // reads one integer from input
Two things to notice:
- You must declare the variable with its type first (
int x;), then read into it.cinuses the type to know how to parse the text. This is the static typing we will dig into in lesson 1-3. >>skips whitespace and newlines, sostd::cin >> a >> b;happily reads3 4from one line or from two separate lines.
Compare with Python, where input() always gives you a string and you convert it yourself with int(...). In C++ the conversion happens automatically based on the variable's type.
Reading a word and a number
cin uses each variable's declared type to decide how to interpret the incoming text. Here one word becomes a std::string and one number becomes an int, from a single line of input.
#include <iostream> #include <string> int main() { std::string name; int age; std::cin >> name >> age; std::cout << name << " is " << age << "\n"; return 0; }
Input
Ada 36Output
Ada is 36The declarations come first and the reads come second, which is the required order. std::cin >> name stops at the space, taking only Ada, and >> age then parses 36 as a number rather than as text.
The extra #include <string> is needed because std::string lives in its own header. Forgetting it is a common early compile error, and the message usually complains that string is not a member of std.
How chained extraction handles whitespace
Given the input 10 20, the statement std::cin >> a >> b; reads 10 into a and 20 into b, discarding the space between them.
The >> operator extracts one value per variable and skips any whitespace it finds first, including spaces, tabs, and newlines. Chaining works for the same reason it works on cout, since each >> returns the stream itself, ready for the next extraction.
That whitespace skipping is why the same statement reads input written on one line or split across two, and it is what makes cin so convenient for the input formats used in interview problems.
The flip side is that
>>cannot read a line containing spaces into a single string, because it stops at the first space. Reading a full line takesstd::getline(std::cin, line)instead, which lesson 8-2 covers alongsidestd::string.
Reading two numbers and printing their sum
Input, calculation, and output in one small program. The two integers are read together, and the addition happens inside the print statement.
#include <iostream> int main() { int a; int b; std::cin >> a >> b; std::cout << "Sum: " << a + b << "\n"; return 0; }
Input
3 4
Output
Sum: 7Writing << a + b inside the chain works because + is evaluated before the value is handed to the stream, so cout receives the finished number 7. Storing it first, as int sum = a + b;, would be equally correct and is worth preferring once an expression grows long enough to be worth naming.
The variables are declared without initial values here, which is safe only because cin fills them on the very next line. Reading an uninitialized variable before assigning to it is one of the classic C++ bugs, and lesson 2-1 comes back to why.