9.06 Formatted Stream Manipulators (<iomanip>), Fixed-Width Alignments & Sensor Summary Reports
Executive Summary: Generating formatted tabular text reports using (std::setw, std::setprecision, std::fixed). We analyze table column alignment, computing running averages/min/max statistics, and generating ASCII telemetry summary logs for embedded serial terminals.
💻 1. Annotated Source Code
#include <iostream> #include <fstream> #include <iomanip> using namespace std; int main() { ifstream infile("salaries.txt"); if (!infile) { cerr << "Error opening file. Aborting..." << endl; return 1; } double salary; int employeeCount = 0; double totalSalary = 0.0; int highEarners = 0; while (infile >> salary) { if (salary < 0 || salary > 1000000) { //skip invalid data continue; } employeeCount++; totalSalary += salary; if (salary > 100000) { highEarners++; } }//end while infile.close(); double averageSalary = (employeeCount > 0) ? totalSalary / employeeCount : 0.0; cout << fixed << showpoint << setprecision(2); cout << "Employee Salary Report" << endl; cout << "-------------------------------" << endl; cout << "Total Employees: " << employeeCount << endl; cout << "Total Payroll: $" << totalSalary << endl; cout << "Average Salary: $" << averageSalary << endl; cout << "Over $100,000 Earners: " << highEarners << endl; return 0; }
55000 72000 98000 121000 45000 105000 86000 34000 145000 99999
📐 2. Architecture & UML Class Model
<<struct>>
EmployeeRecord
Payroll Record
Attributes / Data Members
+employeeName : std::string
+baseSalaryCents : int64_t
+taxWithheldCents : int64_t
Operations / Methods
+getNetSalaryCents() : int64_t const
<<compilation-unit>>
SalaryReportGenerator
Payroll Engine
Attributes / Data Members
-employees : std::vector<EmployeeRecord>
Operations / Methods
+loadSalaries(path: const char*) : void
+printFinancialSummary() : void const
🔗 Architectural Relationships & Hierarchy
SalaryReportGenerator
◆──
aggregates
◆──
EmployeeRecord
📚 3. Core C++ Concepts Deep-Dive
1. Formatted Stream Manipulators (<iomanip>)
std::setw(N): Sets field width for the next item.std::setprecision(N): Sets decimal precision.std::fixed: Formats floating-point numbers with fixed decimal notation.
⚡ 4. Embedded Systems & Hardware Reality
1. Embedded ASCII Telemetry Tables
In satellite and drone serial consoles, formatted ASCII tables provide human-readable sensor health summaries over radio telemetry links.
💡 5. Production-Ready Embedded Refactoring
Lightweight string buffer table formatter:
💡 Production-Ready Refactor
#include <cstdint> #include <cstdio> struct SensorReport { uint8_t id; int16_t temp_c; uint16_t voltage_mv; }; // Compact snprintf formatting: 0 <iomanip> Flash bloat! void formatReportLine(const SensorReport& r, char* out_buf, size_t buf_len) noexcept { snprintf(out_buf, buf_len, "| ID: %02u | Temp: %+03d C | V: %04u mV |\r\n", r.id, r.temp_c, r.voltage_mv); }
📝 Knowledge Verification Quiz
Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.
Q1. Which header file provides stream manipulators like std::setw, std::setprecision, and std::setfill?
Detailed Explanation:
<iomanip> contains formatting manipulators for parameterized stream formatting.
Q2. Why is 'snprintf()' often preferred over '<iomanip>' for formatting text in small microcontroller firmware?
Detailed Explanation:
snprintf() provides bounded, format-string-based output with minimal Flash footprint compared to heavy C++ streams.
Q3. What is the scope of 'std::setw(10)' when applied to a stream (cout << setw(10) << a << b;)?
Detailed Explanation:
std::setw is non-sticky: it affects only the immediately following output token.
Q4. How do you calculate running minimum and maximum values across a stream of numbers in $O(1)$ memory?
Detailed Explanation:
Tracking running min/max requires only two scalar variables updated on each iteration ($O(1)$ space).