【发布时间】:2020-07-20 19:00:18
【问题描述】:
我正在努力使用这个 C++ 程序来获得正确的每月付款金额。我认为我无法完全弄清楚的公式有一些地方做错了,它也弄乱了总回报的输出。我需要一些帮助来弄清楚。我在下面的代码中添加了到目前为止的内容:
#include <iostream>
#include <math.h>
using namespace std;
void mortgage::Mortgage() { // class object
double loanAmount = 0;
double annualInterestRate = 0;
double totalYearsToRepay = 0;
cout << "Enter the amount of the loan:";
cin >> loanAmount;
cout << "Enter annual interest rate in decimal term (example 0.075):";
cin >> annualInterestRate;
cout << "Enter the length of the loan in years:";
cin >> totalYearsToRepay;
cout << "Loan Amount: $" << fixed << loanAmount << endl;
cout << "Annual Interest Rate: $" << fixed << annualInterestRate << endl;
cout << "Total Years to Repay: $" << fixed << totalYearsToRepay << endl;
// Find monthly interest rate.
monthlyInterestRate=annualInterestRate /12;
// Find the total # of payments needed.
numberOfPayments = totalYearsToRepay* 12;
// Find power Factor.
powerFactor = pow(1.0 + monthlyInterestRate, numberOfPayments);
// Find the monthly payment.
monthlyPayment= (loanAmount * monthlyInterestRate)/(1-pow(1 + monthlyInterestRate, totalYearsToRepay));
cout << "Monthly Payment: $" << fixed << annualInterestRate << endl;
// Find the total pay back
totalPayBack = (monthlyPayment) * (numberOfPayments);
cout << "Total Pay Back: $" << fixed << totalPayBack << endl;
};
这段代码我应该得到的输出:
贷款金额:$50000.00
年利率:0.06250
偿还年限:10
每月付款:561.40 美元
总回报:67368.06 美元
这段代码的当前输出:
贷款金额:$50000.000000
年利率:0.062500 美元
总偿还年限:10.000000 美元
每月付款:0.062500 美元
总回报:$-599859.388428
【问题讨论】:
-
您不应将利率除以 100。(示例对应于 7.5%,但您将其设为 0.075%。)
-
年利率 X% 小于月利率 (x / 12)%。
-
@idclev463035818:
annualInterestRate /12 /100没有整数除法,因为它被分组为(annualInterestRate /12) /100。 -
在我的原始程序中,我的月利率 = 年利率/12。很抱歉造成混乱
-
@Batsheba 是的,我的错