我正在尝试编写符合以下规则的抵押贷款计算器:
我的代码抛出了以下错误,我不确定原因:
filename.cpp(36) : error c2181: illegal else without matching if
这是我的代码:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double loanAmount, yearlyIntRate, yearsOfLoan;
void main()
{
double amountPaidBack, interestPaid, monthlyPayment;
double commonTerm, numerator, denominator;
cout << setprecision(2) << fixed << endl;
cout << "Lets figure out how badly you got screwed on your house loan\n Enter your loan amount in dollars." << endl;
cin >> loanAmount;
cout << "Enter your yearly Interest Rate as a percent. ";
cin >> yearlyIntRate;
cout << "Enter your term of loan in years. ";
cin >> yearsOfLoan;
if (loanAmount > 999 && loanAmount < 1000000 && yearlyIntRate> 1.99 && yearlyIntRate < 20.01 && yearsOfLoan <= 30 && yearsOfLoan >= 5)
commonTerm = pow(1 + yearlyIntRate / 1200,
yearsOfLoan * 12),
numerator = (yearlyIntRate / 1200) * commonTerm,
denominator = commonTerm - 1,
monthlyPayment = numerator / denominator * loanAmount,
amountPaidBack = yearsOfLoan * 12 * monthlyPayment,
interestPaid = amountPaidBack - loanAmount;
cout << " Your monthly payment will be.\n " << monthlyPayment;
cout << "\n And you paid " << interestPaid << " in interest over the term of the loan." << endl;
else if (loanAmount < 1000 && loanAmount < 1000001 && yearlyIntRate < 2.00 && yearlyIntRate > 20.00 && yearsOfLoan > 30 && yearsOfLoan < 5)
cout << "That is invalid input data" << endl;
}
答案 0 :(得分:0)
第一个问题是你有几个逗号应该是分号。
第二个问题是以下If语句:
if (loanAmount > 999 && loanAmount < 1000000 && yearlyIntRate> 1.99 && yearlyIntRate < 20.01 && yearsOfLoan <= 30 && yearsOfLoan >= 5)
没有打开/关闭大括号{}
,因此如果条件为真,它将被解析为执行后面的第一行。
因此,在声明中:
else if (loanAmount < 1000 && loanAmount < 1000001 && yearlyIntRate < 2.00 && yearlyIntRate > 20.00 && yearsOfLoan > 30 && yearsOfLoan < 5)
先前的If语句已由解析器处理,而其他if语句与任何If语句无关。
为了纠正这个简单的添加大括号{}
,如果上一个If语句成功,则包含应该执行的代码。