Concept Flow - Basic formatting
Start
Prepare output string
Apply formatting rules
Print formatted output
End
This flow shows how a program prepares and prints formatted output step-by-step.
#include <iostream> #include <iomanip> int main() { std::cout << std::setw(5) << 42 << std::endl; return 0; }
| Step | Action | Formatting Applied | Output Produced |
|---|---|---|---|
| 1 | Start program | None | |
| 2 | Call std::setw(5) | Set field width to 5 | |
| 3 | Print number 42 | Right-align in width 5 | 42 |
| 4 | Print newline | None | |
| 5 | Return 0 | Program ends |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| field_width | default | 5 | 5 | 5 |
| output_buffer | "" | "" | " 42" | " 42\n" |
Basic formatting in C++ uses manipulators like std::setw to set field width. std::setw(n) sets the width for the next output only. By default, output is right-aligned with spaces filling empty width. Use std::left to left-align output. Formatting resets after each output operation.