- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的问题:
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;
关于c++ - 为什么 setw(n) 在这里不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28142676/
我想在 C++ 上显示一个“漂亮”的国家列表及其 ISO 货币代码。 问题是我的数据是法语,并且有重音字符。这意味着 Algolia ,实际上写成“Algérie”,而瑞典变成“Suède”。 map
我需要在控制台上打印一些数据。我的代码是: cout << setw(5) << left << "id" << " | " << setw(10) << left << "computer" <<
我想要一个 setw 接受两个参数并返回最大的一个。这可能吗?我该怎么做呢?不寻找代码只是一些方向会很好,因为我无法在线找到明确的答案。 最佳答案 我不认为你可以重载它,但你当然可以用不同的名称定义你
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我有一个名为 os 的 ostream,我这样使用: os << rec.lastname << " " << rec.firstname << setw(30) << rec.phonenum; 输
我正在尝试使用 NTL 库(一个数论库)来格式化输出。其中一个对象是 GF2X 对象,它是一个表示为一串系数的多项式。一个简单的例子: GF2X a = GF2X(5,1); a++; cout(GF
我的代码有问题,不确定是错误还是我的代码有问题。 #include #include using namespace std; int main() { cout << setfill('*')
我正在尝试使用 setw 来清理程序的输出。我想要“要订购的线轴总数”和输出之间的空格。 编辑这就是我想要的: 这就是我得到的 这是我到目前为止所拥有的:更新代码 /*****************
我正在尝试解析一个文本文件,并使用 setw() 将内容输出到控制台并进行格式化。我的问题是只有第一行的格式正确,其余行默认返回到左侧。 while (test) { cout > price
我写了这样一段代码: int d{ 0 }; cin >> setw(2) >> d; 但似乎setw()对读取整数没有影响。如果是这样,我们如何使用 istream 实现 scanf() 的 %2d
我正在尝试以 hh:mm 格式打印时间,但当时间类似于 01:01 时,它打印为 1:1。这是我的代码: void output(int hour, int min, char ampm){
我想像这样证明输出文本 0x29823d80 0x10019b8 / 0 00000000000000000000000000000001 0x37449e60
#include #include #include using namespace std; ifstream f("atestat.in"); ofstream g("atestat.out");
有没有办法强制setw截断? 假设我想得到输出: blah blah blee le bu blah blah blee 有没有办法让它工作: string foo{"bu blah blah ble
这是我的功能 ostream margain(std::string firstWord) { ostream x; x << std::setw(20) << firstWord;
我正在处理格式,但我没有得到正确的格式。 这是我的代码,这是我得到的输出结果 cout << fixed << setprecision(2) << setw(3) << right << sourc
应该是一个微不足道的问题,但是发现setw只适用于它紧接的后续输出,并不确定如何让它适用于所有后续输出。 例如,对于下面这行代码 cout<
我正在尝试使用设置宽度 setw 将字符串输出到输出文件,但是,我无法使其工作。我有以下示例。 // setw example #include #include #includ
我有这个示例代码块: #include #include #include using namespace std; int main(){ string blank = " ";
在c++中,setw函数用于设置下一次插入操作要使用的字符数作为字段宽度。 C 中是否有任何函数,我的意思是,在标准 C 库中,它做同样的事情? 最佳答案 printf("%5d", 42); 将使用
我是一名优秀的程序员,十分优秀!