0
我正在嘗試在C++中編寫一個程序,該程序需要啓動貸款餘額,年利率和每月付款,並打印一份時間表,顯示每個月之後餘額將在貸款之前已付清或直至60個月已過。C++貸款計算器
//Calculates a loan payment schedule
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double startingBalance = 0.0;
double interestRate = 0.0;
double monthlyPayment = 0.0;
double monthlyBalance = 0.0;
double compountInterest = 0.0;
double balance = 0.0;
int month = 0;
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Starting loan balance: " ;
cin >> startingBalance; //User input starting loan balance
if (startingBalance <= 0){
cout <<"\nPlease enter a positive number: " ;
cin >> startingBalance;
}
cout << "\nAnnual interest rate: " ;
cin >> interestRate; //User input interest rate
if ((interestRate <= 0) || (interestRate > 1)){
cout <<"\nPlease enter an interest rate ranging from 0 to 1: " ;
cin >> interestRate;
}
cout << "\nMonthly payment: " ;
cin >> monthlyPayment; //User input monthly payment
if (monthlyPayment <= 0){
cout <<"\nPlease enter a positive number: " ;
cin >> interestRate;
}
startingBalance = balance;
cout << "\nMonth \t Balance\n" << endl; //Outputs a schedule of payments
while ((balance > 0) || (month < 61))
{
balance += (startingBalance * (interestRate/12));
balance -= monthlyPayment;
month = month++;
cout << month << "\t" << balance << "\n";
}
return 0;
}
我相信陳述正確的,但我不斷收到這些作爲結果
Starting loan balance: 10000.00
Annual interest rate: 0.0525
Monthly payment: 500.00
Month Balance
1 -500.000
2 -1000.00
3 -1500.00
依此類推,直到61個月已經過去了。
編譯所有警告和調試信息('G ++ -Wall -g') 。學習**使用調試器**(可能是'gdb') –
您從未將起始餘額設置爲貸款金額。 – OldProgrammer