Skip to content
Output Formatting
step 1/5

Reading — step 1 of 5

Learn

~3 min readStrings

Output Formatting

Streams made output type-safe; manipulators make it precise. Since every exercise here is graded byte-for-byte, <iomanip> is not optional polish — it's how you make 0.666667 become the 0.67 the grader demands, on command.

The precision pair

#include <iomanip>

double x = 2.0 / 3.0;
std::cout << x << "\n";                                    // 0.666667  (6 sig figs)
std::cout << std::fixed << std::setprecision(2) << x << "\n";  // 0.67

The pair to memorize as a unit: std::fixed << std::setprecision(n) = exactly n digits after the decimal point, rounded (0.666… → 0.67, not truncated). Unpaired, setprecision counts significant figures instead — setprecision(2) alone prints 0.67 for 0.666 but 6.7e+01 for 66.6, which is a genuinely confusing bug to meet unwarned. Graders speak fixed-point; use the pair.

One behavioral note: fixed and setprecision are sticky — set once, they apply to every later float on that stream. Set them at the top of main and forget them; that's the idiom.

std::cout << std::fixed << std::setprecision(2);
std::cout << 3.14159 << " " << 2.5 << "\n";      // 3.14 2.50 — both formatted

(Note 2.50 — trailing zeros appear, exactly what "two decimal places" specs want.)

Width, alignment, fill

For columns and tables:

std::cout << std::setw(8) << 42 << "|\n";                    //       42|
std::cout << std::left << std::setw(8) << 42 << "|\n";       // 42      |
std::cout << std::setfill('0') << std::setw(6) << 42 << "\n"; // 000042

setw is the odd one out: it applies to the next insertion only, then resets (unlike sticky fixed/fill/left). Padding a whole table means a setw before every column — tedious, correct, and exactly how it's done.

Composing a line

Format strings don't exist in stream-land; a formatted line is a chain, and text/values interleave:

std::cout << "Hi, " << name << "! You are " << age << " years old.\n";

Spacing bugs live in the string literals ("Hi," vs "Hi, "), precisely as in every other language's formatting lesson — when output is one space off, the space is missing between quotes, not in the values. For debugging invisible whitespace, print delimiters around suspects: std::cout << "[" << s << "]" makes a trailing space or stray newline instantly visible.

(For completeness: C++20 added std::format and C++23 std::println — Python-style format strings, widely loved. The grader's compiler predates them, and streams remain what you'll read in a decade of existing code — learn streams first, cheer for the future.)

Your exercise: Greeting Card

Fixed text, interpolated values, exact spacing and newlines. The reliable procedure, unchanged across the platform: write the expected output literally, then transcribe it into a << chain — every character of fixed text inside quotes, every value as its own insertion, "\n" where lines end. If decimals appear anywhere in the expected output, the sticky pair goes at the top of main with the precision the examples show. The transcription discipline is the lesson — and it's the last time formatting will ever be the hard part.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…