gpt4 book ai didi

c++ - 我怎样才能让这个函数运行不止一次?

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:21 25 4
gpt4 key购买 nike

首先请注意,我是 C++ 的绝对初学者。所以,请放轻松。

我一直在编写下面的代码,作为今年夏天我的编程方法论类(class)作业的一部分。它是一个银行程序,需要用户输入来计算用户贷款的月数 (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 修改您的 main 循环 --

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 程序将继续,否则它将退出。

关于c++ - 我怎样才能让这个函数运行不止一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30944149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com