【发布时间】:2023-04-05 00:58:01
【问题描述】:
我是 C++ 的新手,遇到了一些麻烦。为此,我正在尝试创建一个抵押计算器。我遇到的问题是它没有打印出正确的每月付款和总还款金额。这是我到目前为止所拥有的:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main () {
double annualInterestRate(0); // yearly interest rate
double loanAmount(0); // the amount of the loan
double monthlyInterestRate(0); // interest rate amount monthly
double numberofPayments(0); // the amount of payments
double totalYearsToRepay(0); // years needed to payback
double totalPayBack(0); // total amount being paid back
double monthlyPayment(0);
while (true) {
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;
loanAmount = loanAmount;
cout << "loanAmount: $" << setprecision(2) << fixed << loanAmount << endl;
annualInterestRate = annualInterestRate;
cout << "annualInterestRate: $" << setprecision(5) << fixed << annualInterestRate << endl;
cout << "totalYearsToRepay: " << setprecision(0) << fixed << totalYearsToRepay << endl;
// find monthly interest rate.
monthlyInterestRate=annualInterestRate / 12;
totalYearsToRepay = totalYearsToRepay;
numberofPayments = totalYearsToRepay* 12;
monthlyPayment = (loanAmount * (monthlyInterestRate) * totalYearsToRepay) / (totalYearsToRepay-1);
cout << "Monthly Payment: $" << setprecision(2) << fixed << monthlyPayment << endl;
totalPayBack = (monthlyPayment) * (numberofPayments);
cout << "Total Pay Back: $" << setprecision (2) << fixed << totalPayBack << endl;
}
}
这是应该打印的示例:
Loan Amount: $50000.00
Annual Interest Rate: 0.06250
Years to repay: 10
Monthly Payment: $561.40
Total Pay Back: $67368.06
我没有得到每月付款和总回报。我不知道数学部分有什么问题。请帮忙!给定的公式是monthly payment= loanAmount * monthlyInterestRate * powerFactor / powerFactor -1,其中powerFactor = (1+monthlyInterestRate)^numberofpayments
.
【问题讨论】:
-
你得到了什么?显示输出。
-
OT:像这样的声明:
loanAmount = loanAmount;什么都不做。 -
我的建议是在编写任何代码之前先用纸和笔弄清楚数学。
-
这就是我一直在尝试做的,但我无法弄清楚。这就是我得到的: 贷款金额:250000.00 美元年利率:0.04750 美元总计年偿还:30 月还款:1023.71 美元总还款:368534.48 美元
-
@drescherjm 所说的......显然数学是错误的。似乎那里应该有一个
pow...
标签: c++ math calculator