← C++ from Scratch
Lesson 3 of 62 · 10 XP
Output: formatting
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
<iomanip> controls how numbers look:
#include <iomanip>
double pi = 3.14159265;
std::cout << std::fixed << std::setprecision(2) << pi << '\n'; // 3.14
std::cout << std::setw(6) << 42 << '|' << '\n'; // " 42|" — width 6, aligned right
std::cout << std::left << std::setw(6) << 42 << '|' << '\n'; // "42 |"
std::cout << std::setfill('0') << std::right << std::setw(3) << 7 << '\n'; // 007
std::fixed+std::setprecision(n)— n digits after the decimal point (stays in effect until you change it);std::setw(n)— the width of only the next value;std::left/std::right— alignment;std::setfill(c)— what to fill with.
Task. Print the table exactly like this (the name — width 8, aligned left; the price — width 6, aligned right, 2 decimal places):
Potion 12.50
Sword 99.90
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.