我一直在为C ++类开发抵押计算器。但是,我被困住了。
我从Nerdwallet获得了以下公式,并尝试在我的程序中实现它:
M = P [i(1 + i)^ n] / [(1 + i)^ n – 1]
M =抵押付款
P =校长
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;
}
但是,当我编译它时,出现以下错误:
我只是不了解错误以及它们为什么在那里。
如果有人可以帮助我,我将不胜感激。
谢谢。
答案 0 :(得分:0)
虽然您的数学公式同时使用[]
和()
来对表达式进行分组,但在C ++中只能以这种方式使用()
。
pow
是一个函数调用,而不是您似乎正在使用的就地运算符。它必须看起来像pow(1 + rate, totalPayments)
。
您的if
也在做分配(=
)而不是比较(==
)。实际上,您的代码将仅跟随第一个if
,因为它将years
设置为15。
答案 1 :(得分:0)
您的抵押贷款公式似乎有误。试试这个
monthlyPayment = homePrice * ((rate * pow(1 + rate, totalPayments)) /
(pow(1.00 + rate, totalPayments) - 1));