SrcForge

Lesson 10

Manipulators

Covers setw, setprecision, fixed, left, right, setfill.

Lesson content

What are manipulators?

Manipulators change stream behavior, not data.

They control how output looks.

  • Fixed decimal places
  • Aligned text columns
  • Hex or octal display
  • Spacing and formatting
  • Line breaks

Word processor analogy: bold, center, font, spacing.

Why do manipulators exist?

Without formatting, output looks messy.

Apple 25
Orange 150
Banana 7

// With formatting:

Item         Price
------------------
Apple          25
Orange        150
Banana          7

Real-world use cases

  • Billing systems
  • Banking software
  • Student report cards
  • Inventory management
  • Financial applications
  • Scientific calculations
  • Console dashboards
  • Data reports

Required headers

Most manipulators live in iomanip.

#include <iomanip>

endl, ws, flush live in iostream.

Types of manipulators

Type              | Purpose                          | Examples
----------------- | -------------------------------- | ----------------------------------------
Without arguments | Simple formatting action         | endl, ends, flush, ws
With arguments    | Accepts params for formatting    | setw(), setprecision(), setfill()

Commonly used manipulators

1. endl

Moves cursor, flushes buffer.

cout << endl;

2. setw()

Sets next field's width.

cout << setw(10);

3. setprecision()

Controls digits shown.

cout << setprecision(3);

4. fixed

Shows floats in fixed-point form.

3.141593
// becomes 3.14 with setprecision(2)

5. scientific

Shows floats in scientific form.

1.234567e+02

6. left

Aligns output left.

7. right

Aligns output right.

8. setfill()

Changes setw's fill character.

*****25

9. hex

Shows integers in hex.

10. oct

Shows integers in octal.

11. dec

Returns integers to decimal.

12. boolalpha

Shows bools as true/false.

13. noboolalpha

Shows bools as 1/0.

Example 1: Using endl

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello";
    cout << endl;
    cout << "World";

    return 0;
}

Example 2: Using setw()

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    cout << setw(10) << "Apple";
    cout << endl;
    cout << setw(10) << "Ball";

    return 0;
}

Example 3: fixed and setprecision()

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    double pi = 3.1415926535;

    cout << fixed;
    cout << setprecision(2);
    cout << pi;

    return 0;
}

Example 4: Left and right alignment

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    cout << left;
    cout << setw(12) << "Name";
    cout << setw(8) << "Age";
    cout << endl;

    cout << right;
    cout << setw(12) << "Alice";
    cout << setw(8) << 20;

    return 0;
}

Example 5: Student report table

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    cout << left;
    cout << setw(15) << "Name";
    cout << setw(10) << "Marks";
    cout << endl;

    cout << setfill('-') << setw(25) << "";
    cout << setfill(' ') << endl;

    cout << setw(15) << "John";
    cout << setw(10) << 91;
    cout << endl;

    cout << setw(15) << "Emily";
    cout << setw(10) << 88;
    cout << endl;

    cout << setw(15) << "David";
    cout << setw(10) << 95;

    return 0;
}

Common mistakes and pitfalls

  • setprecision without fixed. Fix: add fixed too
  • Missing iomanip. Fix: include it
  • setw affects all output. Fix: call before each value
  • Forgetting to reset setfill. Fix: restore with space
  • getline after cin skips input. Fix: use cin.ignore()
// Wrong
cout << setprecision(2) << 123.456;
// Output: 1.2e+02
// Correct
cout << fixed << setprecision(2) << 123.456;
// Output: 123.46

Best practices

  • Include iomanip only when needed
  • Prefer \n over endl usually
  • Pair fixed with setprecision for money
  • Apply setw before each aligned value
  • Restore setfill after temp changes
  • Format for readability, not decoration

When not to use this

  • Building GUIs
  • Formatting HTML or web pages
  • Generating PDF or Word docs
  • Highly customized reports
  • Localization and number formatting

Summary

  • Manipulators format streams, not data
  • Most live in iomanip header
  • endl adds newline, flushes buffer
  • setw sets next field width
  • setprecision controls numeric precision
  • fixed sets decimal digit count
  • left and right align fields
  • setfill changes padding character
  • hex, oct, dec change number base
  • boolalpha prints true and false

Frequently asked questions

What are manipulators in C++?

Control stream formatting behavior.

Which header file is required for manipulators?

Most need iomanip. endl needs iostream.

What is the difference between fixed and setprecision()?

setprecision alone sets significant digits. Fixed adds decimal control.

Why does setw() affect only one value?

Applies only to next output. Call it each time.

When should I use boolalpha?

Use it for readable true/false output.