-1
我正在嘗試編寫一個程序,以便用戶輸入金額,利率和支付次數。貸款和利率程序C++
的代碼是假設輸出是這樣的:
Principal: 120000
Interest rate: 10
Number of payments: 12
No. Balance Instalment Interest Total payment
----------------------------------------------------------------
1 120000.00 10000.00 1000.00 11000.00
2 110000.00 10000.00 916.67 10916.67
3 100000.00 10000.00 833.33 10833.33
4 90000.00 10000.00 750.00 10750.00
5 80000.00 10000.00 666.67 10666.67
6 70000.00 10000.00 583.33 10583.33
7 60000.00 10000.00 500.00 10500.00
8 50000.00 10000.00 416.67 10416.67
9 40000.00 10000.00 333.33 10333.33
10 30000.00 10000.00 250.00 10250.00
11 20000.00 10000.00 166.67 10166.67
12 10000.00 10000.00 83.33 10083.33
----------------------------------------------------------------
120000.00 6500.00 126500.00
但我寫的代碼輸出以下:
Number of payments: 12
No. Balance Instalment Interest Total payment
----------------------------------------------------------------
----------------------------------------------------------------
000Program ended with exit code: 0
這裏是我到目前爲止的代碼。
#include <iostream>
using namespace std;
class Loan
{
public:
Loan(double upphaed, double vextir, int afborganir);
void displaySchedule();
private:
void pay(int i);
double payments();
double interest(int i);
double Balance(int i);
double sumOfInstalments;
double sumOfInterest;
double total;
double principal;
double interestRate;
int numOfPayments;
};
Loan::Loan(double upphaed, double vextir, int afborganir)
{
upphaed = principal;
vextir = interestRate;
afborganir = numOfPayments;
sumOfInstalments = 0.0;
sumOfInterest = 0.0;
total = 0.0;
}
void Loan::pay(int i)
{
sumOfInstalments += payments();
sumOfInterest += interest(i);
total += payments() + interest(i);
}
double Loan::payments()
{
return principal/numOfPayments;
}
double Loan::interest(int i)
{
return Balance(i) * (interestRate/12)/100;
}
double Loan::Balance(int i)
{
return principal - (i - 1) * payments();
}
void readData(double& principal, double& interestRate, int& numOfPayments)
{
cout << "Principal: ";
cin >> principal;
cout << "Interest rate: ";
cin >> interestRate;
cout << "Number of payments: ";
cin >> numOfPayments;
}
void Loan::displaySchedule()
{
cout << "No. Balance Instalment Interest Total payment" << endl;
cout << "----------------------------------------------------------------" << endl;
for (int i = 1; i <= numOfPayments; i++)
{
cout << i << " " << Balance(i) << " " << payments() << " " << interest(i) << " " << (payments() + interest(i));
pay(i);
}
cout << "----------------------------------------------------------------" << endl;
cout << sumOfInstalments << sumOfInterest << total;
}
int main()
{
double amount, interestRate;
int numPayments;
readData(amount, interestRate, numPayments);
Loan myLoan(amount, interestRate, numPayments);
myLoan.displaySchedule();
return 0;
}
使用調試器。在'displaySchedule'的'for'處放置一個斷點。看看'numOfPayments'的值,確認它不是零。 – 2014-10-29 19:23:57