首先让我注意到,我绝对是C ++的入门者。所以,请放轻松。

今年夏天,我一直在编写下面的代码,作为我的编程方法论课程作业的一部分。这意味着它是一个银行程序,它需要用户输入来计算月数(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;

}

最佳答案

使用do while循环修改主文件-

int main(int argc, char *argv[])
{
 char ch='n';
do {
 double Bal;
 double p;
 double n;
 double i;
 double pay;
 double A;
 Loan_Data loan1(p, n, i);
 loan1.MakePayment(pay);
 loan1.PrintAmortizationSchedule();
 printf("Do you want to continue ");
 ch=getchar();
 }while(ch=='y');
return 0;

}


Do while循环重复条件为true时包含的代码,即,如果用户在第1次结束时输入y,则程序将继续执行,否则它将退出。

关于c++ - 如何使此功能运行多次?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30944149/

10-09 05:01