为什么 setw(n) 在这里不起作用?
Why doesn't setw(n) work here?
这是我的问题:
Given three variables, a, b, c, of type double that have already been declared and initialized, write some code that prints each of them in a 15 position field on the same line, in such away that scientific (or e-notation or exponential notation) is avoided. Each number should be printed with 5 digits to the right of the decimal point. For example, if their values were 24.014268319, 14309, 0.00937608, the output would be:
|xxxxxxx24.01427xxxx14309.00000xxxxxxxx0.00938
NOTE: The vertical bar, | , on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!
这实际上是我想要做的事情:
cout << fixed << setprecision(5) << 24.014268319 << setw(5) << 5252.25151516 << endl;
但这会产生以下输出:
24.014275252.25152
显然我没有解释如何正确使用 setw(n)
,有人看到我在这里做错了什么吗?
setw(...)
I/O 操纵器有点棘手,因为它的效果正在重置,即宽度设置回零,在每次调用 <<
之后(在documentation).
中描述的其他内容
您需要多次调用setw(15)
,如下所示:
cout << fixed << setprecision(5) << setw(15) << 24.014268319 << setw(15) << 5252.25151516 << endl;
这是我的问题:
Given three variables, a, b, c, of type double that have already been declared and initialized, write some code that prints each of them in a 15 position field on the same line, in such away that scientific (or e-notation or exponential notation) is avoided. Each number should be printed with 5 digits to the right of the decimal point. For example, if their values were 24.014268319, 14309, 0.00937608, the output would be:
|xxxxxxx24.01427xxxx14309.00000xxxxxxxx0.00938
NOTE: The vertical bar, | , on the left above represents the left edge of the print area; it is not to be printed out. Also, we show x in the output above to represent spaces-- your output should not actually have x's!
这实际上是我想要做的事情:
cout << fixed << setprecision(5) << 24.014268319 << setw(5) << 5252.25151516 << endl;
但这会产生以下输出:
24.014275252.25152
显然我没有解释如何正确使用 setw(n)
,有人看到我在这里做错了什么吗?
setw(...)
I/O 操纵器有点棘手,因为它的效果正在重置,即宽度设置回零,在每次调用 <<
之后(在documentation).
您需要多次调用setw(15)
,如下所示:
cout << fixed << setprecision(5) << setw(15) << 24.014268319 << setw(15) << 5252.25151516 << endl;