首先是集体任务,这样一来,人们会欣赏帮助,而只是想学习的背后。 我不得不按照我守则中所示的利率等计算每月付款,但与我计算不符。 我的产出为<代码>nan,一则认为是而不是编号
。 我一直在试图指出,在什么地方,我会错失,就如何纠正这一问题提出任何建议? 提前感谢。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
//collect payment info
double loanAmount = 0;
double anualRate = 0;
double monthlyIntRate = 0;
int numOfPayment = 0;
double monthlyPayment = 0;
double amountPaidBack = 0;
double interestPaid = 0;
cout << "Enter loan amount: ";
cin >> loanAmount;
cout << "Enter Anual Interest Rate: ";
cin >> anualRate;
cout << "Enter Payments made: ";
cin >> numOfPayment;
//calculate montly payment
monthlyPayment = (loanAmount * pow(monthlyIntRate + 1, numOfPayment) * monthlyIntRate) / ( pow(monthlyIntRate + 1, numOfPayment) - 1);
//calculate amount paid back
amountPaidBack = monthlyPayment * numOfPayment;
//calculate interest paid
monthlyIntRate = anualRate / 12;
interestPaid = monthlyIntRate * numOfPayment;
//split input from calculated output
cout << "-----------------------------
" << endl;
//Display the calulated data
cout << fixed;
cout << setprecision(2);
cout << "Loan Amount: " << setw(15) << "$ "<< right << loanAmount << endl;
cout << "Monthly Interest Rate: " << setw(14) << monthlyIntRate << "%" << endl;
cout << "Number of Payments: " << setw(17) << numOfPayment << endl;
cout << "Montly Payment: " << setw(19) << "$ " << monthlyPayment << endl;
cout << "Amount Paid Back: " << setw(17) << "$ " << amountPaidBack << endl;
cout << "Interest Paid: " << setw(18) << "$ " << interestPaid << endl;
return 0;
产出:
Loan Amount: $ 100000.00
Monthly Interest Rate: 1.00%
Number of Payments: 36
Montly Payment: $ nan
Amount Paid Back: $ nan
Interest Paid: $ 36.00