讓我首先注意到我是C++的絕對BEGINNER。所以,請放輕鬆點我。如何讓這個功能運行多次?
我一直在寫下面的代碼,作爲今年夏天編程方法課程的一部分。這意味着銀行計劃會根據用戶輸入來計算月數(n),利率(i)和用戶貸款的每月支付。然後,該程序應該從用戶處收取一筆付款金額並計算新的餘額。從這裏,它應該打印一份攤銷報告,描述期初餘額,利息支付,主要付款和期末餘額。所有這些工作都很好,但接下來我遇到了麻煩。該程序應該能夠進行多次付款併爲攤銷報告添加額外的行,並且我無法弄清楚如何再次運行「付款」功能以獲得這些額外付款。請幫助!!
此外,我知道爲成員函數設置的參數幾乎是不需要的,因爲它們被用戶輸入替換,但是它們是指導者在賦值指令中所要求的。
再次感謝您給予的任何建議!
#ifndef LOAN_DATA_H
#define LOAN_DATA_H
class Loan_Data
{
private:
double Bal;
double n;
double i;
double A;
double p;
public:
Loan_Data(double p, double n, double i);
void MakePayment(double pay);
void PrintAmortizationSchedule();
};
#endif /* LOAN_DATA_H */
#include <cstdlib>
#include <cmath>
#include <iostream>
#include "Loan_Data.h"
using namespace std;
Loan_Data::Loan_Data(double p, double n, double i)
{
cout << "Enter the loan amount: $";
cin >> this->p;
cout << "Enter the loan length: ";
cin >> this->n;
cout << "Enter your credit score: ";
cin >> this->i;
this->i = this->i/100;
this->i = this->i/12;
this->n = this->n * 12;
Bal = this->p;
A = (this->p * ((this->i * pow(1 + this->i, n))/(pow(1 + this->i, n) - 1)));
cout << "A is: " << A << endl;
cout << "Bal is: " << Bal << endl;
cout << "i is: " << this->i << endl;
}
void Loan_Data::MakePayment(double pay)
{
cout << "i is: " << i << endl;
cout << "Bal is: " << Bal << endl;
cout << "Enter payment first payment amount: $";
cin >> pay;
cout << "Bal is: " << Bal << endl;
Bal = ((i + 1) * Bal) - pay;
A = pay;
}
void Loan_Data::PrintAmortizationSchedule()
{
double iP = (i * Bal);
double pP = (A - (i*Bal));
double endingBalance = ((1 + i)*Bal - A);
double payment2 = (i + 1)*Bal;
cout << "Beginning Bal." << "\t""\t" << cout << "Interest paid" << "\t""\t" << cout << "Principle paid" << "\t""\t" << cout << "Ending Bal." << "\t""\t" << endl;
if ((i + 1)*Bal > A)
{
cout << p << "\t""\t""\t""\t" << iP << "\t""\t""\t""\t" << pP << "\t\t""\t""\t" << endingBalance << endl;
endingBalance = Bal;
}
else if (Bal < A)
{
cout << Bal << "\t""\t""\t""\t" << iP << "\t""\t""\t""\t" << (payment2 - (i*Bal)) << "\t\t""\t""\t" << ((1 + i)*Bal - payment2) << endl;
Bal = ((1 + i)*Bal - payment2);
}
else if (Bal == 0)
{
cout << "0" << "\t""\t""\t""\t""\t" << "0" << "\t""\t""\t""\t""\t" << "0" << "\t\t""\t""\t""\t" << "0" << endl;
}
}
int main(int argc, char *argv[])
{
double Bal;
double p;
double n;
double i;
double pay;
double A;
Loan_Data loan1(p, n, i);
loan1.MakePayment(pay);
loan1.PrintAmortizationSchedule();
return 0;
}
您可以使用[for循環(HTTP:// EN .cppreference.com/w/cpp/language/for)或[while循環](http://en.cppreference.com/w/cpp/language/while) – CoryKramer