抵押计算器验证输入错误

时间:2014-04-20 19:33:30

标签: c++ validation input

我正在尝试编写符合以下规则的抵押贷款计算器:

  • 取3个输入参数
      数据类型double的
    • 1指定贷款金额
    • 2数据类型double指定年利率ex(7.50)
    • 3 of data type int指定偿还贷款的年数。
  • 返回每月付款的值 必须验证参数
  • 金额1千零1百万贷款金额
  • Int率在2-20%之间
  • 5-30岁之间的数字
  • 如果有任何无效应返回-1.0

我的代码抛出了以下错误,我不确定原因:

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;
}

1 个答案:

答案 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语句成功,则包含应该执行的代码。