为什么在函数中传递参数时会出现算术计算错误
Why do errors in arithmetic calculations when passing argument in function occur?
下面的代码编译正确,但计算似乎是问题当打印出第一个扣款"提款"时customerBalance是2.07361e(8032.78-244.0=7788.78)?数据成员是私有的,但这似乎不是问题公开的或私人的。我不确定。任何建议都是欢迎非常感谢。
#include <iostream>
#include "BankAccount.hpp"
using namespace std;
BankAccount::BankAccount(string name, string ID, double balance)
{
customerName = name;
customerID = ID;
customerBalance = balance;
}
string BankAccount::getCustomerName()
{
return customerName;
}
string BankAccount::getCustomerID()
{
return customerID;
}
double BankAccount::getCustomerBalance()
{
return customerBalance;
}
void BankAccount::withdraw(double w)
{
customerBalance = (customerBalance - w);
}
void BankAccount::deposit(double d)
{
customerBalance = (customerBalance + d);
}
int main()
{
double customerBalance;
BankAccount account1("Harry Potter", "K4637", 8032.78);
account1.withdraw(244.0);
cout << customerBalance;
account1.withdraw(3012.58);
account1.deposit(37.54);
account1.withdraw(1807.12);
account1.deposit(500.00);
double finalBalance = account1.getCustomerBalance();
}
您在main
中声明的cusotmerBalance
与BankAccount::customerBalance
不同。您从未初始化customerBalance
,因此在打印时它有一个未定义的值。
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- OpenCV - Python 断言错误:SAD 算法 - 立体相机视差图计算
- 如何在常量计算表达式中获取编译时错误?
- 计算阶乘的 C++17 倍表达式中的错误
- 为什么 2 个双精度值之间的差值计算错误?
- 我在c++中遇到了一个奇怪的错误,其中一个计算2个小整数加法的语句溢出到一个长值中
- 错误 C2064:术语的计算结果不是采用 3 个参数的函数
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- C++ openmp 并行计算计算错误的结果
- 为什么按位运算符在使用与整数中提供的位数相同的位数计算可能的最大范围时会产生错误
- 术语不计算为函数采用 1 个参数错误?
- Travis CI 中奇怪的编译器错误不会在本地计算机上发生
- 点云库:计算筛选关键点 - 输入云错误
- 在线程错误 C2064 中:term 的计算结果不为 0 个参数的函数
- GetAsyncKeyState(VK_RETURN) 错误地计算为 true
- 错误 C2064:term 的计算结果不是采用 1 个参数的函数 - 关于线程的一些东西
- log2 计算在 CPP 中给出了错误的结果
- 错误计算RSI或错误在MetaTrader 4
- c++程序错误计算