我的计算中的计算有什么问题? [英] What is wrong with my calculations in my program?
问题描述
我不太确定我的代码中的哪个位置导致导致错误计算的问题。当我运行该程序时,会出现以下警告:C4305:'argument':从'double'截断到'float'。税金额(ta)和总费用(tc)似乎有问题,谢谢。
当前输出:
税前成本:$ 30.20
税额:$ 30.20
总成本:$ -107374144.00
碎牛肉是 - 海绵
按任意键继续。 。
**应该是什么:
您的项目名称:碎牛肉
税前费用:$ 30.20
税额:$ 2.64
总费用:$ 32.84
碎牛肉是预付的
Im not quite sure where in my code is causing the problem that causes wrong calculations. When I run the program there is a warning of : C4305: 'argument' : truncation from 'double' to 'float'. There seems to be something wrong with Tax amount(ta) and Total cost(tc), Thanks.
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();
}
推荐答案
30.20
税额:
30.20
Tax Amount:
< br>
30.20
总费用:
30.20
Total Cost:
-107374144.00
碎牛肉是预付的
按任意键继续。 。
**应该是什么:
您的项目名称:碎牛肉
税前成本:
-107374144.00
ground beef is ex-sponged
Press any key to continue . .
What it **should** be:
Your item name:ground beef
Cost before Tax:
这篇关于我的计算中的计算有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!