2016-12-02 6 views
-1

私は単純なクラスをC++で計算し、単純な算術演算を行い、コンパイラは自分のdouble変数をintに変換します。私のクラスでは、doubleを返すmakePaymentメソッドがあり、正常に動作しています。問題は私がcharge私のカードにしようとすると、何かのようにdoubleからintへのcharge私のカードの後に​​すべての操作または私がbalanceを印刷するとき、整数を返すので、クラスの変数balanceのように表示されます。C++コンパイラがdoubleからintに変換されます

class DebitCard { 

public: 

    DebitCard(); 

    bool makePayment(double amount); 

    const double& getBalance() 
     { return balance; } 

    void dailyInterest() 
     { balance *= interest; } 

    void chargeCard(double amount) 
     { balance += amount; } 

private: 

    string card_number; 
    short pin; 
    double balance; 
    double payment_fee; // percentage fee for paying with the card 
    double interest;  // daily interest 
    //double charge_tax;  // percentage taxing for charing the card 

}; 

、ここでは、私がメイン機能で行うテストです

DebitCard d; // balance is set to 100 

    d.makePayment(91.50); 
    cout << std::setprecision(3) << d.getBalance() << endl; // 7.58 

    d.chargeCard(200); 
    cout << std::setprecision(3) << d.getBalance() << endl; // 208 

    d.makePayment(91.50); 
    cout << std::setprecision(3) << d.getBalance() << endl; // 115 

私は本当に誰かが私を説明することができれば、それは非常になりますので、起こっていることである理由の周り私の頭をラップすることはできません感謝。

+0

[std :: showpoint、std :: noshowpoint](http://en.cppreference.com/w/cpp/io/manip/showpoint) – crashmstr

+0

http://en.cppreference.com/w/cpp/ io/ios_base/precision: "浮動小数点出力の精度(つまり***何桁の数字が生成されますか***)を管理します..." –

+0

bool makePayment(double amount); ??実装? – eyllanesc

答えて

1

set::precision(3)が出力で3桁を要求しています。

それはあなたが得るものです。

+0

それは、std :: fix it fixed :)素早い返信をありがとう! –

関連する問題