C 抵押付款计算器公式

C++ Mortgage payment calculator formula

本文关键字:计算器 付款      更新时间:2023-10-16

我一直在为C 类的抵押计算器工作。但是,我被卡住了。

我从Nerdwallet中获得了以下公式,并尝试在我的程序中实现它:

m = p [i(1 i)^n]/[(1 i)^n - 1]

m =抵押付款

p = principal

i =兴趣

n =付款数

这是我当前正在使用的代码。

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int argc, char** argv) 
{
    int years, homePrice, creditScore, totalPayments;
    float rate, mortgageTotal, monthlyPayment;
    cout << "What is the price of the home that you are looking to mortgage?n";
    cin >> homePrice; //Assigns the variable homePrice to a value
    cout << "What is your credit score?n";
    cin >> creditScore; //Assigns the creditScore variable a value
    cout << "Would you prefer a 15 year or 30 year mortgage? Please type 15 or 30.n";
    cin >> years;
if ( years = 15 ) //If input is 15 year it will go down this logical path
    {
        if (creditScore >=760) //If their credit rating is equal to or more than 760, their rate will be .043 also nested if.
            { 
                rate = .043;
                cout << "Your interest rate is 4.3%n";
            }
        else if (creditScore >= 700) //If their credit rating is equal to or more than 700, their rate will be .0455
            {
                rate = .0455;
                cout << "Your interest rate is 4.55%n";
            }
        else if (creditScore >= 660) //If their credit rating is equal to or more than 660, their rate will be .048
            {
                rate = .048;
                cout << "Your interest rate is 4.8%n";
            }
        else if (creditScore >= 620) //If their credit rating is equal to or more than 620, their rate will be .058
            {
                rate = .058;
                cout << "Your interest rate is 5.8%n";
            }
        else if (creditScore >= 580) //If their credit rating is equal to or more than 580, their rate will be .0655
            {
                rate = .0655;
                cout << "Your interest rate is 6.55%n";
            }
        else if (creditScore >= 500) //If their credit rating is equal to or more than 500, their rate will be .083
            {
                rate = .083;
                cout << "Your interest rate is 8.3%n";
            }
    }
else if ( years=30 ) 
    {
        if (creditScore >= 760)
            {
                rate=.043;
                cout <<"Your interest rate is 4.3%n";
            }
        else if (creditScore >= 700)
            {
                rate=.0455;
                cout << "Your interest rate is 4.55%n";
            }
        else if (creditScore >= 660)
            {
                rate=.048;
                cout << "Your interest rate is 4.8%n";
            }
        else if (creditScore >= 620)
            {
                rate=.058;
                cout << "Your interest rate is 5.8%n";
            }
        else if (creditScore >= 580)
            {
                rate=.0655;
                cout << "Your interest rate is 6.55%n";
            }
        else if (creditScore >= 500)
            {
                rate=.083;
                cout << "Your interest rate is 8.3%n";
            }
    }
    totalPayments = years * 12;
    monthlyPayment = homePrice * [[rate * (1 + rate)pow(totalPayments)] / [(1 + rate)pow(totalPayments) - 1]];
    mortgageTotal = monthlyPayment * totalPayments;
    cout << "Your mortgage will cost approximately " << mortgageTotal << " and your monthly payment will be " << monthlyPayment << endl;
return 0;

}

但是,当我去编译时,我会收到以下错误:

错误

我只是不明白错误以及为什么在那里。

如果有人可以帮助我,我会非常感谢。

谢谢。

当您的数学公式同时使用[] s和 () s进行分组表达式时,只能以() s的方式使用C 。

pow是一个函数调用,而不是像您似乎正在使用的那样的现场运算符。它需要看起来像pow(1 + rate, totalPayments)

您的if S也在进行分配(=)而不是比较(==)。实际上,您的代码仅遵循第一个if,因为它将years设置为15。

您的抵押公式似乎是错误的。尝试这个

monthlyPayment = homePrice * ((rate * pow(1 + rate, totalPayments)) /  
(pow(1.00 + rate, totalPayments) - 1));