为什么我的计算在我的程序中搞砸了
why are my calculations messed up in my program?
我不太确定我的代码中哪里导致了导致错误计算的问题。当我运行程序时,出现以下警告:
C4305:"参数":从"双精度"到"浮点数"的截断。
税额(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();
}
在构造函数中,您在初始化之前使用它cbt
:
tc=cbt+tax;
cbt = cost*quan;
未初始化变量的值本质上是随机的。
不相关的建议:
使用
std::string
而不是 C 样式字符串(char
数组(。在浮动文本上使用
f
后缀,为它们提供类型float
而不是double
(从而删除警告(:7.55f
而不是7.55
、0.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;
}
你在初始化之前使用的是cbt
:
tc=cbt+tax;
cbt = cost*quan;
交换这两行,它至少应该可以工作。
首先你计算tax
:
tax=cost*quan;
其中成本 == x == 7.55 和泉 == y == 4。所以 7.55 * 4 是 30.2。这就是你得到的输出。如果你期望有别的东西,可以修复你的计算。
第二:
tc=cbt+tax;
cbt = cost*quan;
使用未初始化的cbt
计算tc
,然后将值分配给cbt
。所以你会得到垃圾tc
警告原因:
默认情况下,7.55, 4, 8.75
是双精度。应将它们指定为浮点数:7.55f, 4.0f, 8.75f
计算错误的原因:
查看@Angew和@PaulEvans的答案
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 我的程序没有从文件中读取并输入数据
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 为什么我的程序不能显示斐波那契级数?
- 为什么我的程序在使用预留后没有加速?
- 为什么如果我添加这一行,我的程序会不断询问值
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- 为什么我的 c++ 程序检查不是初始化的变量?
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- C++ - 为什么我的程序在再次调用函数后关闭?
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 我需要如何更改我的程序以使用打开/关闭原则?
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较