まずこれはクラスの割り当てですので、私は助けていただければ幸いです。私は、私のコードに見られるように、金利などに基づいて毎月の支払いを計算する必要がありますが、何かが私の計算とは異なります。私の出力はnot a number
と思われる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 << "-----------------------------\n" << 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
デバッガの使い方を知っていますか?どの計算がnansを生成しているかを調べるには、コードをステップ実行する必要があります。 –
デバッガを使用してコードをステップ実行し、各ステップで変数の値を調べようとしましたか? – suszterpatt
ナノは実際には数字ではありません。毎月の支払いのために計算のさまざまな部分をプリントアウト( 'cout')して、計算がどこに行くのかを調べるのはなぜでしょうか。 –