-1

我有以下代码片段:

int main()
{
  int x, R, m;
  double Y, D, P;

  cout << "Enter investment: ";
  cin >> x;

  cout << "Enter rate per year: ";
  cin >> R;

  cout << "Enter months: ";
  cin >> m;

  /* Convert to years. */
  Y = m / 12;

  D = x * pow ((1 + (R/100)), Y);

  P = D - x;

  cout << "Investment after " << Y << " years is " << D << " bucks.\n";
  cout << "Profit is " << Y << " year(s) is " << P << " bucks.\n";

我的 cout 结果计算不正确,由于某种原因,我没有得到小数点。

我怀疑它与数据类型有关。

4

2 回答 2

2

使至少一个操作数加倍以使其工作。

  Y = m / 12.;

  D = x * pow ((1 + (R/100.)), Y);
于 2013-09-18T01:13:25.580 回答
1
D = x * pow ((1 + (R/100)), Y);

R/100将产生一个整数,因为两个操作数都是整数。至少其中之一必须是double它的工作。

D = x * pow ((1 + (R/100.0)), Y);
于 2013-09-18T01:12:10.657 回答