2014-10-30 61 views
2

我不太清楚我的代碼在哪裏導致導致錯誤計算的問題。當我運行該程序時出現以下警告:爲什麼我的計算在我的程序中搞砸了?

C4305:'argument':從'double'截斷爲'float'。

似乎是壞了稅額(TA)和總成本(TC),

Current Output: 
Cost before Tax: $30.20 
Tax Amount: $30.20  
Total Cost: $-107374144.00 
ground beef is ex-sponged 
Press any key to continue . . 


What it **should** be: 
Your item name:ground beef 
Cost before Tax: $30.20 
Tax Amount: $2.64 
Total Cost: $32.84 
ground beef is ex-sponged 

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream> 
#include<iomanip> 
#include<string> 

using namespace std; 

class item 
{ 
public: 
    item(char* = " " ,float=0.0,int=0,float=0.0); 
    ~item(); 
    void print(); 
    void calc(int); 
private: 
    char name[20]; 
    int quan; 
    float cost, tp, cbt, tax, tc; 
}; 
item::~item() 
{ 
    cout << name << " is ex-sponged"<<endl; 
    system("pause"); 
    } 
item::item(char *w,float x, int y, float z) 
{ 
    strcpy(name, w); 
    cost = x; 
    quan=y; 
    tp = z; 
    tax=cost*quan; 
    tc=cbt+tax; 
    cbt = cost*quan; 
} 
void item::print() 
{ 
    cout << "Your item name:" << name << endl; 
    cout << "Cost before Tax: $" << cbt << endl; 
    cout << "Tax Amount: $" << tax << endl; 
    cout << "Total Cost: $" << tc << endl; 
} 

void item::calc(int n) 
{ 
    quan += n; 
    cbt = cost*quan; 
    tax = cbt*tp/100; 
    tc = cbt + tax; 
} 

int main() 
{ 
    item i("ground beef", 7.55, 4, 8.75); 
    cout << setprecision(2) << showpoint << fixed; 
    i.print(); 
} 
+3

最大的錯誤是你使用浮點數來表示錢。 – user2079303 2014-10-30 14:57:10

+0

前海綿被拼寫刪除,雖然我喜歡你的版本更好。 – interjay 2014-10-30 14:59:04

+2

另外,在這裏使用C字符串沒有什麼好的理由(畢竟你使用'cout')。使用C++'std :: string'。 – 2014-10-30 15:01:31

回答

0

原因警告:

通過默認7.55, 4, 8.75是雙打。你應該將其指定爲浮動:7.55f, 4.0f, 8.75f

原因誤算了一筆賬:

通過@Angew看到答案,@PaulEvans

+0

這是事實,但不是虛假總成本價值的原因。 – 2014-10-30 14:59:59

+0

正確,增加免責聲明。我只是在警告中回答。 – RvdK 2014-10-30 15:01:23

3

您使用cbt它的初始化之前:

tc=cbt+tax; 
cbt = cost*quan; 

交換這兩條線,它至少應該工作。

+0

你快了幾秒鐘,有一個+1 :-) – Angew 2014-10-30 15:09:07

7

在構造函數中,你使用cbt你初始化之前:

tc=cbt+tax; 
cbt = cost*quan; 

的未初始化變量的值是隨機的。


無關建議:

  • 使用std::string而不是C風格的字符串(char陣列)。

  • 使用f浮動文字後綴給他們鍵入float而不是double(從而刪除警告)的7.55f代替7.550.0f(或0.f),而不是0.0等。

  • 不要使用浮點格式的金錢,而是使用固定精度格式。舍入誤差和貨幣申請中的不準確性爲不好。

  • 在聲明中命名參數,它用作自我記錄代碼。

  • 通常,最好在構造函數中使用mem-initialiser-lists而不是在構造函數體中分配成員。這對於具有非平凡默認構造函數的類類型的成員特別相關(並且對於不能被默認初始化的成員而言是必需的)。由於數據成員總是按照課堂內聲明的順序進行初始化,所以您必須重新排序。

我不知道一個定點格式的副手,但與其他建議,你的代碼應該是這樣的:

class item 
{ 
public: 
    item(std::string name = " " , float cost = 0.0, int quant = 0, float tp = 0.0); 
    ~item(); 
    void print(); 
    void calc(int); 
private: 
    std::string name; 
    float cost; 
    int quan; 
    float tp, tax, cbt, tc; 
}; 

item::~item() 
{ 
    cout << name << " is ex-sponged" << endl; 
    system("pause"); 
} 

item::item(std::string name, float cost, int quant, float tp) 
    : name(name), 
    cost(cost), 
    quan(quant), 
    tp(tp), 
    tax(cost * quant), 
    cbt(cost * quant), 
    tc(cbt + tax) 
{ 
} 

void item::print() 
{ 
    cout << "Your item name:" << name << endl; 
    cout << "Cost before Tax: $" << cbt << endl; 
    cout << "Tax Amount: $" << tax << endl; 
    cout << "Total Cost: $" << tc << endl; 
} 

void item::calc(int n) 
{ 
    quan += n; 
    cbt = cost*quan; 
    tax = cbt*tp/100; 
    tc = cbt + tax; 
} 
+0

但是,你解決了我腦海中響起的所有警告鈴聲,讓你直接回復+1;) – 2014-10-30 15:16:40

1

首先你計算tax

tax=cost*quan; 

其中成本== x == 7.55和權== == == 4.所以7.55 * 4是30.2。這就是你得到的輸出。如果你期望別的東西在那裏修正你的計算。

二:

tc=cbt+tax; 
cbt = cost*quan; 

你使用未初始化cbt計算tc,然後分配價值cbt。所以你得到垃圾tc