让我首先注意到我是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