【问题标题】:c++ mortgage loan calculatorc++ 抵押贷款计算器
【发布时间】: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


【解决方案1】:

我会使用这里提到的公式https://www.thecalculatorsite.com/articles/finance/compound-interest-formula.php

然后我将 A 值除以该人必须支付的总价格(公式中称为 A)的月数。

您正在寻找所谓的“复合利率”。

【讨论】:

  • 谢谢!我试过了,但它没有给我应该得到的每月支付和总支付的金额
【解决方案2】:

在发布的代码中有这一行

monthlyPayment = (loanAmount * (monthlyInterestRate) * totalYearsToRepay)
               / (totalYearsToRepay-1);

在 cmets 中,OP 清除

给出的公式是:
月供 = 贷款金额 * 月利率 * powerFactor / powerFactor - 1
在哪里
powerFactor = (1+ 月利率) ^ 支付次数

这可以翻译成那些 C++ 代码行:

double powerFactor = std::pow(1.0 + monthlyInterestRate, numberofPayments);
double monthlyPayment = loanAmount * monthlyInterestRate * powerFactor
                      / (powerFactor - 1.0);
//  The parenthesis, here, are needed, due to standard math operator precedence

值得注意的是代码包含其他问题,例如分配给自己的变量或永无止境的循环

while (true)
{
    // Put a break statement somewhere or change the condition
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 2020-12-13
    • 2015-06-30
    • 2020-07-20
    • 2013-06-10
    相关资源
    最近更新 更多