Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / C++ Programming Tutorial / Chapter 1: Foundations & Modern C++ Syntax
CPP ⏱ 10 min read

Input, Output, and Formatting Manipulators

C++ I/O streams are type-safe and format-flexible, using stream operators and `<iomanip>` manipulators.

What is it?

`std::cin` is the standard input stream (using extraction operator `>>`), `std::cout` is output, and `<iomanip>` provides formatting manipulators (`std::setw`, `std::setprecision`, `std::fixed`).

Why is it used?

Eliminates format specifier mismatch bugs from C (like accidentally passing a double to `%d`) and produces aligned tabular reports.

Detailed Explanation

Stream Manipulators from `<iomanip>`: • `std::fixed`: Displays floating-point numbers in fixed-point notation. • `std::setprecision(n)`: Sets number of decimal places. • `std::setw(n)`: Sets field width for the next output item. • `std::left` / `std::right`: Aligns output text. • Reading full lines: `std::getline(std::cin, str)` reads entire lines containing whitespace (unlike `std::cin >> str` which stops at the first space).

Syntax Structure

Syntax Blueprint
#include <iomanip>
std::cout << std::fixed << std::setprecision(2) << 2500.5;

Code Example

CPP Code
#include <iostream>
#include <iomanip>
#include <string>

int main() {
    // Academic Course Catalog Report
    std::cout << "==================================================\n";
    std::cout << std::left << std::setw(8)  << "ID"
              << std::left << std::setw(24) << "Course Name"
              << std::right << std::setw(12) << "Tuition Fee"
              << "\n";
    std::cout << "--------------------------------------------------\n";
    
    // Format monetary figures to 2 decimal places
    std::cout << std::fixed << std::setprecision(2);
    
    std::cout << std::left << std::setw(8)  << "#101"
              << std::left << std::setw(24) << "Advanced Python"
              << std::right << std::setw(12) << 2500.00 << "\n";
              
    std::cout << std::left << std::setw(8)  << "#102"
              << std::left << std::setw(24) << "C & C++ Programming"
              << std::right << std::setw(12) << 2500.00 << "\n";
              
    std::cout << std::left << std::setw(8)  << "#103"
              << std::left << std::setw(24) << "Data Analytics Pack"
              << std::right << std::setw(12) << 5397.00 << "\n";
    std::cout << "==================================================\n";
    
    return 0;
}

Explanation of the Code

• `std::setw(n)` defines fixed-width column widths. • `std::left` and `std::right` manage column alignment. • `std::fixed` and `std::setprecision(2)` format currency values with two decimal digits.

Expected Output

================================================== ID Course Name Tuition Fee -------------------------------------------------- #101 Advanced Python 2500.00 #102 C & C++ Programming 2500.00 #103 Data Analytics Pack 5397.00 ==================================================

Real-World Application

Used across financial terminals, flight booking logs, and diagnostic console dashboards.

Common Mistakes to Avoid

• Mixing `std::cin >> val` with `std::getline()` (the newline character left in the buffer by `>>` causes `getline()` to read an empty line! Use `std::cin.ignore()` to clear it). • Forgetting that `std::setw()` only applies to the immediately following item.

Quick Practice Challenge

Print the number 123.4567 formatted to exactly 2 decimal places using `<iomanip>`.

Key Takeaways

• C++ streams are type-safe and automatically detect data types. • Use `<iomanip>` manipulators (`std::setw`, `std::setprecision`) for clean formatting. • Use `std::getline(std::cin, str)` to read full strings with whitespace.