A total of five pounds shows as “5”, and setprecision(2) can put scientific notation in a receipt. Three lines fix it — and one distinction explains every crooked table you will ever write.
setprecision(2) alone can produce scientific notationYou have worked out a total. It is five pounds exactly. You print it, and C++ shows you this:
Total: 5 # not 5.00 — and an assignment asking for currency will be marked down for it
Two things are happening, and neither is obvious. A double holding 5.0 prints as 5, because trailing zeros are not significant. And C++ shows about six significant digits by default — so 1234.5678 comes out as 1234.57 without you asking for anything at all.
Add the include at the top, then set it once before you print:
This is the one that costs an evening. Used alone, setprecision(2) means two significant figures — not two decimal places. Verified on a real compiler:
cout << setprecision(2) << 1234.5678; 1.2e+03 # scientific notation. two significant figures. cout << fixed << setprecision(2) << 1234.5678; 1234.57 # two DECIMAL places. what you meant.
fixed is the word that changes the meaning from “significant figures” to “places after the point”. Leave it out and you can get scientific notation in a receipt, which looks like the program is badly broken when it is doing exactly what you said.
Every line below is what the real compiler prints. Try setprecision without fixed.
Once numbers look right, the next assignment asks for a table. Three tools, and they behave differently from each other in a way nobody warns you about.
setw(n) sets a minimum width. left and right decide which side the padding goes. setfill('.') changes the padding character from a space.
setw applies to the very next thing printed, and then forgets. Everything else — fixed, setprecision, left, setfill — stays switched on until you change it.
So you set the decimals once, and you repeat setw before every single column. Verified — with one setw(10) at the front, only the first name is padded and the rest run together.
Ask C++ to show 2.675 to two decimal places and you get 2.67, not 2.68. That is not a rounding rule you have forgotten — it is level 8 arriving again.
The number stored is not exactly 2.675. It is a hair below it, because a two-state machine cannot write that value exactly, so rounding to two places honestly gives 2.67. Nothing here is broken and there is no flag that fixes it — which is one more reason to keep money in whole pennies as an int, and only divide by 100 at the very moment you print.
For coursework, fixed and setprecision(2) are what your professor is asking for and are entirely fine. Just do not be surprised by a half-penny, and do not spend an hour hunting a bug that is really the machine telling the truth.
Write the cout line for a receipt row: item name on the left, price on the right, aligned in a column.
Hand over the values that break it. A very long item name. A price of exactly 5. A price of 1234.5. Zero. A negative.
The interesting failure is the long name: setw is a MINIMUM, not a maximum, so a name wider than the column pushes everything after it out of line and nothing complains. That is a boundary case in the level-11 sense, and it is the reason real receipts truncate long names.
Without fixed you get significant figures — and scientific notation in your receipt.
setw is one-shot. Everything else stays on until changed. This is why tables go crooked.
Something wider than the column pushes the rest out of line, silently.
Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.
Have a real go before revealing. Being wrong here is worth more than being right in three weeks.
A total of exactly five pounds. What does this print?
This is the trap. What comes out?
Now with the fix.
Both names are inside one setw(6). What does the line look like? (Use spaces exactly.)
← back to the whole C++ track · stuck on anything? ask peter.