首先请注意,我是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;
}
答案 0 :(得分: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;
}
当条件为真时,while循环重复你所包含的代码,即,如果用户在第一次结束时输入y,程序将继续,否则它将退出。
答案 1 :(得分:0)
如果您想根据&#34;条件&#34;多次运行一段代码,请使用c++ while loop或c++ for loop.
while(condition){
execute function;
}
for(variable; condition; variable modification){
execute function;
}