我在使用 std::fixed 时缺少什么
What is missing from my use of std::fixed?
我正在编写一个程序来模拟plinko板,在该程序中,我有一个输出分配给每个插槽的资金的函数。我正在尝试使用 std::fixed 和 std::setprecision 输出每个小数点后有两个零的值,但每个值都输出,就好像我根本没有使用 fixed 一样。我错过了什么?这是我的代码:
#include <iostream>
#include <iomanip>
using namespace std;
void ChipsAllSlots(int numChips) {
const int NUM_SLOTS = 9;
const int slotWinnings[NUM_SLOTS] = {100, 500, 1000, 0, 10000, 0, 1000, 500, 100};
for (int i = 0; i < numChips; ++i) {
cout << fixed << setprecision(2) << slotWinnings[i] << endl;
}
}
int main() {
ChipsAllSlots(9);
return 0;
}
std::fixed
和std::setprecision
只影响浮点数的输出。你输出整数,所以这些操纵器不起作用。如果在插入流之前转换为浮点数,或者首先使用浮点数组,则会看到缺少.00
。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- 在 c++ 中,没有 **std::fixed** 的 **std::setprecision()** 的作用是什么?
- 我在使用 std::fixed 时缺少什么
- 真的,"fixed" I/O 操纵器的反面是什么?