cpp

formatting

02_formatting.cpp⚙️
/**
 * ============================================================
 * C++ OUTPUT FORMATTING
 * ============================================================
 * 
 * This file covers advanced output formatting using <iomanip>:
 * - setw, setfill
 * - setprecision, fixed, scientific
 * - left, right, internal
 * - showpos, showpoint, uppercase
 * - hex, dec, oct
 * 
 * Compile: g++ -std=c++17 -Wall 02_formatting.cpp -o formatting
 * Run: ./formatting
 * 
 * ============================================================
 */

#include <iostream>
#include <iomanip>    // For setw, setprecision, etc.
#include <string>
#include <cmath>      // For M_PI, sqrt, etc.

using namespace std;

int main() {
    cout << "============================================" << endl;
    cout << "     OUTPUT FORMATTING IN C++" << endl;
    cout << "============================================" << endl << endl;

    // ========================================================
    // PART 1: WIDTH AND FILL - setw, setfill
    // ========================================================
    
    cout << "--- PART 1: WIDTH AND FILL ---" << endl << endl;
    
    // setw - set field width (applies to NEXT output only)
    cout << "Default:" << endl;
    cout << 42 << endl;
    cout << 123 << endl;
    cout << 7 << endl;
    
    cout << "\nWith setw(10) - right aligned by default:" << endl;
    cout << setw(10) << 42 << endl;
    cout << setw(10) << 123 << endl;
    cout << setw(10) << 7 << endl;
    
    // setfill - fill empty spaces with character
    cout << "\nWith setw(10) and setfill('0'):" << endl;
    cout << setfill('0');
    cout << setw(10) << 42 << endl;
    cout << setw(10) << 123 << endl;
    cout << setw(10) << 7 << endl;
    
    // Reset fill character
    cout << setfill(' ');
    
    // Table example
    cout << "\nFormatted Table:" << endl;
    cout << setfill('-') << setw(40) << "" << setfill(' ') << endl;
    cout << setw(15) << left << "Name" 
         << setw(10) << right << "Age" 
         << setw(15) << right << "Salary" << endl;
    cout << setfill('-') << setw(40) << "" << setfill(' ') << endl;
    cout << setw(15) << left << "Alice" 
         << setw(10) << right << 28 
         << setw(15) << right << 75000 << endl;
    cout << setw(15) << left << "Bob" 
         << setw(10) << right << 35 
         << setw(15) << right << 92000 << endl;
    cout << setw(15) << left << "Charlie" 
         << setw(10) << right << 42 
         << setw(15) << right << 120000 << endl;
    cout << setfill('-') << setw(40) << "" << setfill(' ') << endl;
    
    cout << endl;

    // ========================================================
    // PART 2: ALIGNMENT - left, right, internal
    // ========================================================
    
    cout << "--- PART 2: ALIGNMENT ---" << endl << endl;
    
    int num = -42;
    
    cout << "right (default): [" << setw(10) << right << num << "]" << endl;
    cout << "left:            [" << setw(10) << left << num << "]" << endl;
    cout << "internal:        [" << setw(10) << internal << num << "]" << endl;
    // internal puts fill between sign and number
    
    // Reset to right alignment
    cout << right;
    
    cout << endl;

    // ========================================================
    // PART 3: FLOATING POINT PRECISION
    // ========================================================
    
    cout << "--- PART 3: FLOATING POINT PRECISION ---" << endl << endl;
    
    double pi = 3.14159265358979323846;
    double bigNum = 1234567.89;
    double smallNum = 0.000123456789;
    
    // Default precision (6 significant digits)
    cout << "Default (6 significant digits):" << endl;
    cout << "pi = " << pi << endl;
    cout << "bigNum = " << bigNum << endl;
    cout << "smallNum = " << smallNum << endl;
    
    // setprecision - total significant digits (default mode)
    cout << "\nsetprecision(3) - 3 significant digits:" << endl;
    cout << setprecision(3);
    cout << "pi = " << pi << endl;
    cout << "bigNum = " << bigNum << endl;
    
    // fixed - fixed decimal notation
    cout << "\nfixed + setprecision(2) - 2 decimal places:" << endl;
    cout << fixed << setprecision(2);
    cout << "pi = " << pi << endl;
    cout << "bigNum = " << bigNum << endl;
    cout << "smallNum = " << smallNum << endl;
    
    // scientific - scientific notation
    cout << "\nscientific + setprecision(4):" << endl;
    cout << scientific << setprecision(4);
    cout << "pi = " << pi << endl;
    cout << "bigNum = " << bigNum << endl;
    cout << "smallNum = " << smallNum << endl;
    
    // Reset to default
    cout << defaultfloat << setprecision(6);
    
    // Currency formatting example
    cout << "\nCurrency Formatting:" << endl;
    double price = 1234.5;
    cout << "Price: $" << fixed << setprecision(2) << price << endl;
    
    // Reset
    cout << defaultfloat << setprecision(6);
    
    cout << endl;

    // ========================================================
    // PART 4: NUMERIC BASE - dec, hex, oct
    // ========================================================
    
    cout << "--- PART 4: NUMERIC BASE ---" << endl << endl;
    
    int value = 255;
    
    cout << "Value: " << value << endl;
    cout << "Decimal:     " << dec << value << endl;
    cout << "Hexadecimal: " << hex << value << endl;
    cout << "Octal:       " << oct << value << endl;
    
    // With showbase to display prefix
    cout << "\nWith showbase:" << endl;
    cout << showbase;
    cout << "Decimal:     " << dec << value << endl;
    cout << "Hexadecimal: " << hex << value << endl;
    cout << "Octal:       " << oct << value << endl;
    cout << noshowbase;
    
    // Uppercase hex letters
    cout << "\nUppercase hex:" << endl;
    cout << uppercase << showbase << hex << value << endl;
    cout << nouppercase << noshowbase << dec;
    
    cout << endl;

    // ========================================================
    // PART 5: BOOLEAN AND SIGN DISPLAY
    // ========================================================
    
    cout << "--- PART 5: BOOLEAN AND SIGN ---" << endl << endl;
    
    bool flag = true;
    cout << "Boolean (default): " << flag << endl;
    cout << "Boolean (boolalpha): " << boolalpha << flag << endl;
    cout << "Boolean (noboolalpha): " << noboolalpha << flag << endl;
    
    int positive = 42;
    int negative = -42;
    
    cout << "\nSign display:" << endl;
    cout << "Default: " << positive << ", " << negative << endl;
    cout << showpos;
    cout << "showpos: " << positive << ", " << negative << endl;
    cout << noshowpos;
    
    // showpoint - always show decimal point
    double whole = 100.0;
    cout << "\nDecimal point:" << endl;
    cout << "Default: " << whole << endl;
    cout << showpoint << "showpoint: " << whole << noshowpoint << endl;
    
    cout << endl;

    // ========================================================
    // PART 6: PRACTICAL EXAMPLES
    // ========================================================
    
    cout << "--- PART 6: PRACTICAL EXAMPLES ---" << endl << endl;
    
    // Receipt formatting
    cout << "========== RECEIPT ==========" << endl;
    cout << left << setw(20) << "Item" << right << setw(10) << "Price" << endl;
    cout << setfill('-') << setw(30) << "" << setfill(' ') << endl;
    cout << left << setw(20) << "Coffee" 
         << right << "$" << setw(9) << fixed << setprecision(2) << 4.50 << endl;
    cout << left << setw(20) << "Sandwich" 
         << right << "$" << setw(9) << 8.99 << endl;
    cout << left << setw(20) << "Cookie" 
         << right << "$" << setw(9) << 2.25 << endl;
    cout << setfill('-') << setw(30) << "" << setfill(' ') << endl;
    cout << left << setw(20) << "TOTAL" 
         << right << "$" << setw(9) << (4.50 + 8.99 + 2.25) << endl;
    cout << "=============================" << endl;
    
    cout << defaultfloat << setprecision(6);  // Reset
    
    // Memory address formatting
    cout << "\nMemory Addresses:" << endl;
    int arr[] = {1, 2, 3};
    for (int i = 0; i < 3; i++) {
        cout << "arr[" << i << "] at " << hex << showbase << &arr[i] 
             << " = " << dec << arr[i] << endl;
    }
    cout << noshowbase;
    
    cout << endl;

    // ========================================================
    // SUMMARY TABLE
    // ========================================================
    
    cout << "============================================" << endl;
    cout << "FORMATTING MANIPULATORS SUMMARY:" << endl;
    cout << "============================================" << endl;
    cout << "setw(n)       - Set field width" << endl;
    cout << "setfill(c)    - Set fill character" << endl;
    cout << "setprecision(n) - Set precision" << endl;
    cout << "fixed         - Fixed decimal notation" << endl;
    cout << "scientific    - Scientific notation" << endl;
    cout << "defaultfloat  - Reset float format" << endl;
    cout << "left/right    - Alignment" << endl;
    cout << "internal      - Sign-padded alignment" << endl;
    cout << "dec/hex/oct   - Numeric base" << endl;
    cout << "showbase      - Show 0x, 0 prefixes" << endl;
    cout << "showpos       - Show + for positive" << endl;
    cout << "showpoint     - Always show decimal" << endl;
    cout << "boolalpha     - true/false for bools" << endl;
    cout << "uppercase     - Uppercase hex/sci" << endl;
    cout << "============================================" << endl;

    return 0;
}

// ============================================================
// EXERCISES:
// ============================================================
/*
 * 1. Create a multiplication table (1-10) with aligned columns
 * 
 * 2. Write a program that displays a bank statement with:
 *    - Transaction descriptions
 *    - Amounts (properly formatted currency)
 *    - Running balance
 * 
 * 3. Display the same number in decimal, binary, octal, and 
 *    hexadecimal formats
 * 
 * 4. Create a function that prints a progress bar:
 *    [=====>    ] 50%
 */
Formatting - C++ Tutorial | DeepML