我正在模拟贷款支付计算器,并且确定使用了正确的方程式和数据类型。我是否缺少数据类型转换之类的东西?我在做C ++不允许做的事情吗?
我尝试对方程中的变量重新排序,更改变量和函数的数据类型,将方程移到函数之外。
float annualInterestRate,
payment,
periodRate = annualInterestRate / 1200.0;
int loanAmount,
years,
months = years * 12;
int mortgageLoanMinimum = 100000,
carLoanMinimum = 5000,
carLoanMaximum = 200000;
float mortgageRateMinimum = 2.0,
mortgageRateMaximum = 12.0,
carRateMinimum = 0.0,
carRateMaximum = 15.0;
int mortgageTermMinimum = 15,
mortgageTermMaximum = 30,
carTermMinimum = 3,
carTermMaximum = 6;
float mortgage() {
cout << "How much money do you want to borrow? (Nothing less than $100,000): ";
cin >> loanAmount;
cout << "How much annual interest rate by percent? (2.0% - 12.0%): ";
cin >> annualInterestRate;
cout << "For how many years? (15 - 30 Years): ";
cin >> years;
payment = (loanAmount * periodRate * pow((1 + periodRate), months)) / (pow((1 + periodRate), months));
return(payment);
}
选择抵押贷款时,为creditAmount输入500000,为AnnualInterestRate输入4.5,为年输入30,我希望还款额为2533.80,但始终为0。
答案 0 :(得分:0)
全局变量在C ++中被初始化为0。
这样做的时候
int loanAmount,
years,
months = years * 12;
years
初始化为0,而months
初始化为0 * 12 =0。由于您从未将months
的值更新为不为0,因此计算将始终0。
答案 1 :(得分:0)
线条
float annualInterestRate,
payment,
periodRate = annualInterestRate / 1200.0;
int loanAmount,
years,
months = years * 12;
不要做我想让他们做的事。
periodicRate
和months
被初始化为0。但是,当您从用户输入中读取annualInterestRate
和years
的值时,它们不会被更新。
在读取了periodicRate
和months
之后,您需要计算annualInterestRate
和years
。
float mortgage() {
cout << "How much money do you want to borrow? (Nothing less than $100,000): ";
cin >> loanAmount;
cout << "How much annual interest rate by percent? (2.0% - 12.0%): ";
cin >> annualInterestRate;
cout << "For how many years? (15 - 30 Years): ";
cin >> years;
float periodRate = annualInterestRate / 1200.0;
int months = years * 12;
payment = (loanAmount * periodRate * pow((1 + periodRate), months)) / (pow((1 + periodRate), months));
return(payment);
}
进行适当的更改后,您可以删除全局变量periodicRate
和months
。
答案 2 :(得分:0)
我怀疑您误会了
months = years * 12;
实际上在工作。
执行该语句时,它将months
的值设置为years
当前值的12倍。它并没有告诉计算机月份总是 应该是years
值的12倍。现在,当语句运行时,years
的值尚未设置,years * 12 == 0
也是如此。
您可以在获取months
的用户输入后,通过为years
分配一个值或用months
替换计算中的12 * years
来修正代码。
答案 3 :(得分:0)
您将periodRate声明为
periodRate = annualInterestRate / 1200.0;
但是,当声明periodRate时,annulaInterestRate是编译器初始化的值,即0.0f,这也意味着periodRate = 0.0f。在这里,您需要使用define
#define periodRate (annualInterestRate / 1200.0)
几个月也一样。
#define months (years * 12)