C++ Tutorial

I/O Streams

std::cout, std::cin, std::cerr, and standard libraries

What are C++ Streams?

In C++, we don't have built-in "print" or "input" keywords. Instead, we use I/O Streams from the Standard Library (<iostream>). Think of a stream as a physical flow of data from your program to the console (output) or from the user to your program (input).
cpp
#include <iostream>

int main() {
    // std::cout = Standard Console Output!
    std::cout << "Streaming data to console..." << std::endl;
    return 0;
}

Direction Operators (<< and >>)

  • << (Insertion Operator): Inserts data into the stream (e.g., cout << "Hi").
  • >> (Extraction Operator): Extracts data from the stream (e.g., cin >> age).
Wait! The direction of the arrows shows where the data is going. << points to cout; >> points to a variable.

Basic Output: std::cout

You can chain multiple pieces of data together in one line.
cpp
int age = 25;
std::cout << "User is " << age << " years old." << std::endl;

std::endl vs. \n

  • std::endl: Ends the line AND "flushes" the stream (forces all data to be printed immediately). This is slightly slower.
  • \n: Just ends the line. This is much faster for high-volume text.

Basic Input: std::cin

The std::cin stream pauses the program and waits for the user to type something.
cpp
int x;
std::cout << "Enter a number: ";
std::cin >> x;

std::cout << "You entered: " << x << std::endl;

Reading Full Lines: std::getline()

std::cin stops reading at the first space! To read a whole sentence, use std::getline().
cpp
#include <string>

std::string name;
std::getline(std::cin, name); // Reads spaces correctly!

Other Standard Streams

StreamNamePurpose
std::coutStandard OutputFor normal program messages.
std::cinStandard InputTo receive data from the user.
std::cerrStandard ErrorUnbuffered error messages (prints immediately).
std::clogStandard LogBuffered error messages (for slow logging).

Formatting Streams (<iomanip>)

You can control how your output looks (e.g., how many decimal places) by using the <iomanip> library.
cpp
#include <iomanip>

double price = 12.34567;
std::cout << std::fixed << std::setprecision(2) << price; // 12.35

Summary

FeatureCodePurpose
Outputstd::cout << xPrint data
Inputstd::cin >> xGet data (Single word/number)
Full Linestd::getline(cin, s)Get data (Full sentence)
Errorstd::cerr << errPrint errors immediately
Precisionstd::setprecision(n)Control decimal places
Best ForConsole apps, simple I/O, debugging
Flushstd::endlEnsure text is printed NOW
BufferedStreams store data in a buffer before printing
Library#include <iostream>The required library!
Redirectstdout/stderr can be redirected to files!
EfficiencyUse \n instead of endl in fast loops
Chaincout << a << b << c;Piece together messages
Hex/Decstd::hex, std::dec (Format numbers!)
Boolstd::boolalpha (Print true/false instead of 1/0!)
Widthstd::setw(n) (Align columns perfectly!)
Namespaceusing namespace std; (Avoids typing std::)
Cautionusing namespace std can cause name collisions!
C StandardInclude <cstdio> for C's printf (Old but very fast!)
Formatstd::format (The modern, high-speed way in C++20!)