我使用std :: fixed时缺少什么?

时间:2017-10-30 07:04:33

标签: c++

我正在编写一个程序来模拟plinko板,并且在该程序中我有一个输出分配给每个插槽的钱的功能。我尝试使用std :: fixed和std :: setprecision在小数位后输出每个值两个零,但每个值都输出,好像我没有使用固定的。我错过了什么?这是我的代码:

#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;
}

1 个答案:

答案 0 :(得分:6)

IEquatable<T>std::fixed仅影响浮点数的输出。您输出整数,因此这些操纵器无效。如果在插入流之前转换为浮点数,或者首先使用浮点数组,那么您将看到std::setprecision您丢失的内容。