C++ 出纳功能不支持';不适用于特定情况

C++ 出纳功能不支持';不适用于特定情况,c++,C++,这是我第一次在这里发帖,所以如果代码太长,我会非常乐意编辑它 我在做一个出纳,一个可以改变的功能 奇怪的是: 如果我以1.15英镑买1.14英镑,我会得到一便士的退款。 但如果我以0.15买0.14,我就得不到一分钱了 另外,如果我以0.20的价格买0.15,我会得到一枚五分镍币。 但是如果我以0.25买0.20,我可以拿回4便士。我需要帮助理解为什么我要拿回4便士,而不是我应该拿回的0便士和1便士。我相信分区肯定出了什么问题 #include <iostream> #include

这是我第一次在这里发帖,所以如果代码太长,我会非常乐意编辑它

我在做一个出纳,一个可以改变的功能

奇怪的是: 如果我以1.15英镑买1.14英镑,我会得到一便士的退款。 但如果我以0.15买0.14,我就得不到一分钱了

另外,如果我以0.20的价格买0.15,我会得到一枚五分镍币。 但是如果我以0.25买0.20,我可以拿回4便士。我需要帮助理解为什么我要拿回4便士,而不是我应该拿回的0便士和1便士。我相信分区肯定出了什么问题

#include <iostream>
#include <cmath>

using namespace std;

int main() {

  double buyAmount = 0.0;
  double paidAmount = 0.0;
  double diffAmount = 0.0;
  double smallChange = 0.0;
  int dollars = 0;
  int quarters = 0;
  int dimes = 0;
  int nickels = 0;
  int pennies = 0;

  cout << "Enter purchase amount: ";
  cin >> buyAmount;
  cout << endl;

  cout << "Enter paid amount: ";
  cin >> paidAmount;
  cout << endl;

  diffAmount = paidAmount - buyAmount;

  if (diffAmount < 0 ) {
     cout << "Error: Make sure paid amount exceeds purchase amount. ";
     cout << endl;
  }

  dollars = floor(diffAmount);
  smallChange = (diffAmount - dollars) * 100  

  quarters = (smallChange / 25);
  smallChange = smallChange - (quarters * 25);
  dimes = (smallChange / 10);
  smallChange = smallChange - (dimes * 10);
  nickels = floor(smallChange / 5);
  smallChange = smallChange - (nickels * 5);
  pennies = floor(smallChange / 1); 

  cout << "Total Change: $" << diffAmount;
  cout << endl << endl;
  cout << "dollars " << dollars << endl;
  cout << "quarters " << quarters << endl;
  cout << "dimes " << dimes << endl;
  cout << "nickels " << nickels << endl;
  cout << "pennies " << pennies << endl;
}
#包括
#包括
使用名称空间std;
int main(){
双倍购买金额=0.0;
双paidAmount=0.0;
双扩散量=0.0;
双倍微小变化=0.0;
整数美元=0;
四分之一整数=0;
int-dimes=0;
整数镍币=0;
整数便士=0;
不能>购买金额;
cout paidAmount;

cout浮点运算不精确。有时会出现小错误。例如,0.25/0.25可能导致1或0.9999999999。
如果您使用floor,则除非您添加额外的“epsilon”以补偿计算错误,否则这将不起作用,例如:

quarters = floor(smallChange / 0.25);
可能成为

const double epsilon = 1E-5;
quarters = floor(smallChange / 0.25 + epsilon);

INT仍然是管理货币的更好方法:

int diffAmount = ...; // (pennies or cents).
int dollars = diffAmount / 100;
int smallChange = diffAmount % 100;
int quarters = smallChange / 25;
smallChange -= quarters * 25;
int dimes = smallChange / 10;
smallChange -= dimes * 10;
...

您还应该让数据驱动:创建一个包含不同硬币及其值的数据结构,然后编写代码在该数据结构中循环。这样可以很容易地切换到另一种货币(大多数没有四分之一硬币)或修复错误。

您不应该使用浮点,因为它们不精确

相反,将美元和美分分别读入整数,然后完全转换成美分。
整数除法将使这项工作完美。
如果您只是假设用户将正确输入,您可以这样阅读:

int purchaseDollars = 0;
int purchaseCents = 0;
char decimalPoint;  // Can be anything except a number in this snippet.
std::cin >> purchaseDollars >> decimalPoint >> purchaseCents;
int buyAmount = purchaseCents + 100 * purchaseDollars; // Convert to cents
// Same for paid amount

int diffAmount = paidAmount - buyAmount;
int dollars = diffAmount / 100;   // Integer division makes this work as you want
diffAmount -= dollars;            // Now diffAmount is just the cents
/// and so on...

快速提示:不要对金额使用
double
,使用int并存储便士数。这将使计算更容易(无需设置下限,…)@Nicolas Dedranoux我试图解决的一个问题是将零钱乘以100,然后将所有小数移到整数,并移走所有浮点数。它仍然不起作用。但我会编辑它,让它看起来像未来的读者。使用浮点数作为货币是非常不受欢迎的,甚至可能因为iss而被禁止由@NicolasDefranoux@user139323另请参阅:问题在于,当您将24.99999999974转换为int时,浮点是24,而不是25。最简单的解决方案当然是不使用浮点。但如果您确实想使用它,请投资一个将舍入到最接近的整数的函数。如果我t是两个小数之间的差?我试过了,但它不起作用。我算出了。
diffAmount=static\u cast((paidAmount-buyAmount)*100+0.5)
的想法是始终将值管理为int,将paidAmount和byAmount输入为int(便士数)顺便说一句,当收银员键入金额时,他们不键入小数点,只需输入以便士表示的值即可;)---如果将其转换为int,效果与下限相同。