C++ replaces C's printf/scanf with a type-safe stream-based I/O system built around << (insertion) and >> (extraction).
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
cout << "Enter your name: ";
getline(cin, name); // reads a full line, including spaces
cout << "Enter your age: ";
cin >> age; // reads a single token, stops at whitespace
cout << "Hello " << name << ", age " << age << "!" << endl;
return 0;
}
Unlike printf, where mismatching a format specifier and argument type (%d for a float) causes undefined behavior, cout << x automatically calls the correct output logic for whatever type x actually is — there's no format string to get wrong, and no risk of a type mismatch crashing the program.
cin >> variable reads a single whitespace-delimited token and stops at the first space, tab, or newline, which is why it can't capture a full name with a space in it — getline(cin, str) reads an entire line instead, including spaces, up to the newline. A common beginner trap: mixing cin >> age followed immediately by getline(cin, name) — the >> operator leaves the trailing newline character in the input buffer, which getline then immediately consumes as an "empty" line; call cin.ignore() between them to skip that leftover newline.
endl flushes the output buffer in addition to inserting a newline, which is slightly slower than "\n" alone — prefer "\n" in performance-sensitive loops and reserve endl for when an explicit flush is actually needed.