Skip to main content
Module 1 · Lesson 3 of 33/26 in the course~12 min
Module lessons (3/3)

Input/Output and streams

Explore Stage

Typed streams to consoles and pipes

Read and format data on standard I/O without running into slow buffers or broken inputs.

Unlike C (printf/scanf based on fragile format strings), C++ manages I/O through type-oriented streams: the compiler checks from the start that the data passed to << or extracted with >> has a valid serialization.

1. std::cout and Concatenation with <<

The << operator inserts values into the standard output stream and returns the reference to the stream itself, allowing chained calls:

C++
#include <iostream>

int main() {
    int level {3};
    double fps {59.9};
    std::cout << "Livello " << level << " | " << fps << " FPS\n";
    return 0;
}

2. \n vs std::endl: Buffer Performance

Writing to terminal or file costs a lot of system calls, so std::cout accumulates bytes in a buffer. std::endl does two things: prints '\n' and forces immediate flushing (flush).

C++
#include <iostream>

int main() {
    // Veloce nei cicli: nessun flush forzato ad ogni iterazione
    for (int i = 0; i < 5; ++i) {
        std::cout << i << '\n';
    }
    // Utile solo quando serve vedere subito un log o svuotare errori:
    std::cerr << "Log critico immediato" << std::endl;
    return 0;
}

3. Read Values with std::cin >> and Rows with std::getline

std::cin >> var skips leading whitespace and stops at the first space or newline. If the user types "Mario Rossi" in a single string, std::getline(std::cin, line) is needed:

C++
#include <iostream>
#include <string>

int main() {
    int age {0};
    std::string fullName;

    std::cin >> age;
    std::getline(std::cin >> std::ws, fullName); // std::ws consuma gli a-capo residui!

    std::cout << fullName << " (" << age << ")\n";
    return 0;
}

4. Formatting with <iomanip> (std::fixed, std::setprecision)

To print monetary amounts or textual booleans, the <iomanip> header exposes persistent or targeted manipulators:

C++
#include <iomanip>
#include <iostream>

int main() {
    double price {19.5};
    bool ok {true};

    std::cout << std::fixed << std::setprecision(2) << price << " EUR\n";
    std::cout << std::boolalpha << "Stato: " << ok << '\n';
    return 0;
}

Practical exercises

Exercise#cpp.m1.l3.e1
Attempts: 0Loading…

Prints the message "C++ is fast!" followed by std::endl to the standard output stream.

Loading editor…
Show hint

Concatenate std::cout << "C++ is fast!" << std::endl;.

Solution available after 3 attempts

Exercise#cpp.m1.l3.e2
Attempts: 0Loading…

Declare int number;, read a value with std::cin, and then print it to std::cout.

Loading editor…
Show hint

Write int number; std::cin >> number; std::cout << number;.

Solution available after 3 attempts

Exercise#cpp.m1.l3.e4
Attempts: 0Loading…

Include <iomanip> and print double total = 42.5; formatted exactly to 2 decimal places with std::fixed and std::setprecision(2) followed by \n.

Loading editor…
Show hint

Add #include <iomanip> and then std::cout << std::fixed << std::setprecision(2) << total << "\n";.

Solution available after 3 attempts

Check for understanding

Quiz#cpp.m1.l3.e3
Ready

Why is '\n' preferred to std::endl when printing thousands of lines in a loop?

C++
for (int i = 0; i < 10000; ++i) {
    std::cout << i << '\n';
}
Answer options
Quiz#cpp.m1.l3.e5
Ready

What does name read if the user enters 7\nAnna Verdi in the following program?

C++
int id;
std::string name;
std::cin >> id;
std::getline(std::cin, name);
Answer options