2014-10-30 64 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