私はPlinkoボードをシミュレートするプログラムを書いています。そのプログラム内には、各スロットに割り当てられたお金を出力する機能があります。私はstd :: fixedとstd :: setprecisionを使用して各値を小数点以下2桁で出力しようとしていますが、固定値を使用していないかのように各値が出力されています。私は何が欠けていますか?ここに私のコードは次のとおりです。std :: 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;
}
'slotWinnings []'は 'int'型です。 'int'値を出力するとき' fixed'と 'setprecision()'は考慮されません。小数点と2桁の数字が必要な場合は、単に "" .00 "'を表示してください。 – Scheff