Module lessons (3/3)
Input/Output and streams
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:
#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).
#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:
#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:
#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
Prints the message "C++ is fast!" followed by std::endl to the standard output stream.
Show hint
Concatenate std::cout << "C++ is fast!" << std::endl;.
Solution available after 3 attempts
Declare int number;, read a value with std::cin, and then print it to std::cout.
Show hint
Write int number; std::cin >> number; std::cout << number;.
Solution available after 3 attempts
Include <iomanip> and print double total = 42.5; formatted exactly to 2 decimal places with std::fixed and std::setprecision(2) followed by \n.
Show hint
Add #include <iomanip> and then std::cout << std::fixed << std::setprecision(2) << total << "\n";.
Solution available after 3 attempts
Check for understanding
Why is '\n' preferred to std::endl when printing thousands of lines in a loop?
for (int i = 0; i < 10000; ++i) {
std::cout << i << '\n';
}What does name read if the user enters 7\nAnna Verdi in the following program?
int id;
std::string name;
std::cin >> id;
std::getline(std::cin, name);