Programming Language & Its Applications β Virtual Functions and File Handling, NEC licence examination syllabus (Nepal Engineering Council).
Controlling width, precision, base and alignment through the stream's own state.
width() resets after one use but precision() does not, so a "fix" applied once quietly stops applying. Search "floating point output precision C++" and then "why 0.1 + 0.2 != 0.3" β the second explains why setprecision is showing you a rounding of a value that was never exact.#include <iostream>
int main() {
/* width is NOT sticky */
std::cout << " width once: |";
std::cout.width(8);
std::cout << 42 << 7 << "|\n"; /* only 42 is padded */
std::cout << " width each: |";
std::cout.width(8); std::cout << 42;
std::cout.width(8); std::cout << 7;
std::cout << "|\n";
/* precision IS sticky */
std::cout.precision(3);
std::cout << " precision 3: " << 3.14159 << " "
<< 2.71828 << " " << 1.41421 << "\n";
/* default precision counts SIGNIFICANT digits;
with fixed it counts DECIMAL PLACES */
std::cout.precision(4);
std::cout << " default(4 sig): " << 1234.5678 << "\n";
std::cout.setf(std::ios::fixed, std::ios::floatfield);
std::cout << " fixed(4 places): " << 1234.5678 << "\n";
std::cout.setf(std::ios::scientific, std::ios::floatfield);
std::cout << " scientific: " << 1234.5678 << "\n";
std::cout.unsetf(std::ios::floatfield);
/* fill and alignment */
std::cout.fill('.');
std::cout.setf(std::ios::left, std::ios::adjustfield);
std::cout << " left : |"; std::cout.width(12);
std::cout << "Ram" << "|\n";
std::cout.setf(std::ios::right, std::ios::adjustfield);
std::cout << " right : |"; std::cout.width(12);
std::cout << "Ram" << "|\n";
std::cout.setf(std::ios::internal, std::ios::adjustfield);
std::cout << " intern: |"; std::cout.width(12);
std::cout << -456 << "|\n";
std::cout.fill(' ');
/* number bases */
std::cout.setf(std::ios::showbase);
std::cout << " 255 as dec/oct/hex: ";
std::cout.setf(std::ios::dec, std::ios::basefield);
std::cout << 255 << " ";
std::cout.setf(std::ios::oct, std::ios::basefield);
std::cout << 255 << " ";
std::cout.setf(std::ios::hex, std::ios::basefield);
std::cout << 255 << " ";
std::cout.setf(std::ios::uppercase);
std::cout << 255 << "\n";
std::cout.unsetf(std::ios::uppercase);
std::cout.setf(std::ios::dec, std::ios::basefield);
std::cout.unsetf(std::ios::showbase);
/* showpos, showpoint, boolalpha */
std::cout.setf(std::ios::showpos);
std::cout << " showpos: " << 42 << " " << -42 << "\n";
std::cout.unsetf(std::ios::showpos);
std::cout.setf(std::ios::showpoint);
std::cout << " showpoint: " << 5.0 << "\n";
std::cout.unsetf(std::ios::showpoint);
std::cout.setf(std::ios::boolalpha);
std::cout << " boolalpha: " << true << " " << false << "\n";
return 0;
}
Output:
width once: | 427|
width each: | 42 7|
precision 3: 3.14 2.72 1.41
default(4 sig): 1235
fixed(4 places): 1234.5678
scientific: 1.2346e+03
left : |Ram.........|
right : |.........Ram|
intern: |-........456|
255 as dec/oct/hex: 255 0377 0xff 0XFF
showpos: +42 -42
showpoint: 5.00000
boolalpha: true false
fixed plus precision(2) β never precision alone. Every currency-formatting bug in beginner C++ code is this rule not being known.
#include <iostream>
#include <string>
#include <vector>
struct Item { std::string name; int qty; double price; };
int main() {
std::vector<Item> cart = {
{"Notebook", 12, 45.50},
{"Pen (blue)", 50, 8.75},
{"Scientific calculator", 2, 1250.00},
{"Geometry box", 5, 325.25}
};
/* header */
std::cout.setf(std::ios::left, std::ios::adjustfield);
std::cout.width(24); std::cout << "Item";
std::cout.setf(std::ios::right, std::ios::adjustfield);
std::cout.width(5); std::cout << "Qty";
std::cout.width(11); std::cout << "Price";
std::cout.width(13); std::cout << "Amount" << "\n";
std::cout << std::string(53, '=') << "\n";
/* money: fixed AND precision(2) β both needed */
std::cout.setf(std::ios::fixed, std::ios::floatfield);
std::cout.precision(2);
double grand = 0;
for (const auto &it : cart) {
double amt = it.qty * it.price;
grand += amt;
std::cout.setf(std::ios::left, std::ios::adjustfield);
std::cout.width(24); std::cout << it.name;
std::cout.setf(std::ios::right, std::ios::adjustfield);
std::cout.width(5); std::cout << it.qty;
std::cout.width(11); std::cout << it.price;
std::cout.width(13); std::cout << amt << "\n";
}
std::cout << std::string(53, '-') << "\n";
std::cout.setf(std::ios::left, std::ios::adjustfield);
std::cout.width(40); std::cout << "Subtotal";
std::cout.setf(std::ios::right, std::ios::adjustfield);
std::cout.width(13); std::cout << grand << "\n";
double vat = grand * 0.13; /* Nepal VAT 13% */
std::cout.setf(std::ios::left, std::ios::adjustfield);
std::cout.width(40); std::cout << "VAT @ 13%";
std::cout.setf(std::ios::right, std::ios::adjustfield);
std::cout.width(13); std::cout << vat << "\n";
std::cout.setf(std::ios::left, std::ios::adjustfield);
std::cout.width(40); std::cout << "TOTAL";
std::cout.setf(std::ios::right, std::ios::adjustfield);
std::cout.width(13); std::cout << grand + vat << "\n";
return 0;
}
Output:
Item Qty Price Amount
=====================================================
Notebook 12 45.50 546.00
Pen (blue) 50 8.75 437.50
Scientific calculator 2 1250.00 2500.00
Geometry box 5 325.25 1626.25
-----------------------------------------------------
Subtotal 5109.75
VAT @ 13% 664.27
TOTAL 5774.02
FormatGuard above has to exist at all. C++20's std::format fixes it by putting the format in the call itself: std::format("{:>13.2f}", amt) affects nothing else, ever. It is also faster and catches format errors at compile time. Search "std::format format specification syntax"; the mini-language is borrowed from Python and worth learning once for both languages.fixed, so currency needs both. List the flag groups and their masks (basefield, floatfield, adjustfield) and explain why setf takes a mask. Be able to write an aligned table using width, fill and the alignment flags.Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Loadingβ¦