c++ - 为什么我的计算在我的程序中搞砸了?

标签 c++

我不太确定我的代码中的什么地方导致了导致错误计算的问题。当我运行该程序时,会出现以下警告:

C4305: 'argument' : truncation from 'double' to 'float'.

Tax amount(ta) 和 Total cost(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();
}

最佳答案

在构造函数中,您在初始化之前使用 cbt:

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

未初始化变量的值本质上是随机的。


不相关的建议:

  • 使用 std::string 而不是 C 风格的字符串(char 数组)。

  • 在 float 文字上使用 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;
}

关于c++ - 为什么我的计算在我的程序中搞砸了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26656169/

相关文章:

c++ - 为什么在这种情况下 lambda 没有转换为函数?

c++ - 从控制台线程读取或获取命令

c++ - 嵌套模板类的问题

C++ unicode字符打印

c++ - 以与数据库无关的方式查询构建

c++ - 另一个线程中的 pthread_mutex_lock 和 pthread_mutex_lock

c++ - 更改另一个类的值

c++ - 初学者和 C++ 模板 : Is it an how possible using C++ template make a class oriented to work with chars work with costume structures?

C++11 - 引用的初始化不是表达式(的一部分)吗?

c++在范围末尾出现双重释放或损坏错误