计算贷款计算器程序总利息的问题(C ++)

时间:2014-02-06 21:37:15

标签: c++

我一直在为我的c ++课程开发一个简单的贷款计算器程序,我不能为我的生活弄清楚我哪里出错了。除了如何计算贷款的总利息外,整个计划按预期运作。我觉得这里的答案很简单,但我看不到它。

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    int numberOfPayments;
        double monthlyInterestRate;
        double Loan;
        double monthlyPayment;
        double annualInterestRate;
        double interestTotal;
        double loanTotal;


    cout << "Enter the loan amount (principal) -->  ";
    cin >> Loan;
    cout << "Enter the YEARLY interest rate as a percentage -->  ";
    cin  >> annualInterestRate;
    cout << "Enter the number of payments -->  ";
    cin >> numberOfPayments;

    monthlyInterestRate = annualInterestRate / 12;
    monthlyInterestRate = monthlyInterestRate / 100;
    monthlyPayment = (monthlyInterestRate)  *   pow((1 + monthlyInterestRate), numberOfPayments) /  (pow((1 + monthlyInterestRate), numberOfPayments) - 1) * Loan; // Amount of monthly payments
    loanTotal = (monthlyPayment * numberOfPayments); // Total amount due for loan
    interestTotal = monthlyInterestRate * numberOfPayments  * monthlyPayment; 


    cout << monthlyInterestRate << endl;
    cout << "Loan Amount:                                    $" << setw(8) << Loan << endl;
    cout << "Yearly Interest Rate:                            " << setw(8) << annualInterestRate << "%" << endl;           
    cout << "Number of Payments:                              " << setw(8) << numberOfPayments << endl;
    cout << "Monthly Payment:                                $" << setw(8) << setprecision(5) << monthlyPayment << endl;
    cout << "Amount Paid Back:                               $" << setw(8) << setprecision(7) << loanTotal << endl;
    cout << "Interest Paid:                                  $" << setw(8) << setprecision(5) << interestTotal  << endl;
    cout << "\n\nProgram Over";
    cout << "\n\n\nPress Enter to end -->";
    cin.ignore();
    cin.get();  
    return 0;
}

3 个答案:

答案 0 :(得分:1)

interestTotal = monthlyInterestRate * numberOfPayments  * monthlyPayment;

这应该是

interestTotal = (numberOfPayments * monthlyPayment) - Loan;

numberOfPayments * monthlyPayment为您提供实际付款的总金额。您只需要减去借来的金额(Loan),以确定哪些部分是您感兴趣的。

答案 1 :(得分:0)

interestTotal = monthlyInterestRate * numberOfPayments  * monthlyPayment; 

这不正确。总利息不需要乘以monthlyPayment。你需要一个不同的计算。

这不是一个真正的C ++问题,是吗?

答案 2 :(得分:0)

这是一个数学和编程问题。

@ m24p指出利息计算错误,应予以改进 如

interestTotal = (numberOfPayments * monthlyPayment) - Loan; // (@dvnrrs)

但是节目应该按照便士的财务计算。 (或任何货币单位)。每月付款的初始计算需要四舍五入。每月利息,付款和余额都需要四舍五入。然后,最终结果通常与完整double数学不同几美分。理想情况下,这将以最低货币单位完成,以避免重复* 100和/ 100.

每月支付10万美元和360美元,可获得0.26美元的总利息 最后一笔付款,361个月,为0.45美元。

  monthlyPayment = round(monthlyPayment * 100)/100;
  double RealInterestTotal = 0.0;
  unsigned Month = 0;
  while (Loan > 0.0){
    Month++;
    double InterstThisMonth = round(Loan*monthlyInterestRate*100)/100;
    RealInterestTotal = R(RealInterestTotal + InterstThisMonth);
    double PaymentOnLoan = R(monthlyPayment - InterstThisMonth);
    if (PaymentOnLoan > Loan) {
      PaymentOnLoan = Loan;
    }
    Loan = R(Loan - PaymentOnLoan);
  }
  cout << "Real Interest Paid:                             $" << setw(8)
          << setprecision(8) << RealInterestTotal << endl;
  cout << "Months:                                          " << setw(8)
          << setprecision(8) << Month << endl;

为了解决@Zac Howland提出的一个好点,上面使用round()并没有清楚地显示我建议进行四舍五入的两个条件:
1)计算具有分数分,如monthlyPaymentInterstThisMonth
2)使用舍入是因为double的典型二进制64性质代表美元而不是美分。重写为使用函数R(),如果以美分计算,这将是一个无操作。

有趣的是Vancouver Stock Exchange