gpt4 book ai didi

c++ - 贷款计算器变更

转载 作者:行者123 更新时间:2023-11-28 08:27:48 25 4
gpt4 key购买 nike

我创建了一个计算贷款的程序,但它不符合教授要求的指导方针。可以告诉我正确的改动。源代码会很棒并且可以节省时间,但您不必这样做。

问题是:

编写一个程序,让用户输入贷款金额和贷款期限(年数),并显示每个利率从 5% 到 8% 的每月还款额和总还款额,增量为 1/8。这是一个示例运行:

Loan amount: 10000 [Enter]
Numbers of Years: 5: [Enter]

Interest rate Monthly Payment Total Payment
5% 188.71 11322.74
5.125% 189.28 11357.13

这是我之前的代码:

# include <iostream>
# include <iomanip>
# include <cmath>

using namespace std;

int main ()
{
double loanAmountA;
double annualRate;
double paymentAmount;
double amountInterest;
double ratePeriod;
double balanceAfter;
double amountApplied;
double balance;
double paymentPeriod;
int paymentsPerYear;
int totalPayments;
int loanCount = 1;
int paymentCount = 1;
bool anotherLoan = true;
char response;

while (anotherLoan == true)
{
cout<<"Enter amount of loan A:$ ";
cin>>loanAmountA;
cout<<endl;
cout<<"Enter annual percentage rate (APR): "<<"%";
cin>>annualRate;
cout<<endl;
cout<<"Enter the number of payments per year: ";
cin>>paymentsPerYear;
cout<<endl;
cout<<"Enter the total number of payments: ";
cin>>totalPayments;
cout<<endl;
cout<<"Payment Payment Amount Amount to Balance after";
cout<<endl;
cout<<"Number Amount Interest Principal This Payment";
cout<<endl;

cin.ignore(80,'\n');

while (paymentCount <=totalPayments)
{
annualRate = annualRate / 100;
balance = loanAmountA - totalPayments * paymentAmount;
ratePeriod = balance * annualRate;
paymentAmount = loanAmountA * (totalPayments / paymentsPerYear * annualRate) / totalPayments;
balanceAfter = balance - paymentAmount;
balance = loanAmountA - (paymentCount * paymentAmount);


cout<<left<<setprecision(0)<<setw(3)<<paymentCount;
cout<<setw(13)<<left<<fixed<<setprecision(2)<<paymentAmount;
cout<<setw(26)<<left<<fixed<<setprecision(2)<<ratePeriod;
cout<<setw(39)<<left<<fixed<<setprecision(2)<<balance;
cout<<setw(42)<<left<<fixed<<setprecision(2)<<balanceAfter;

if (paymentCount % 12 == 0)
{
cout<<endl;
cout<<"Hit <Enter> to continue: "<<endl;
cin.ignore(80,'\n');
cin.get();
}
paymentCount++;
loanCount++;
cout<<endl;
}

cout<<"Would you like to calculate another loan? y/n and <enter>";
cin>>response;
if (response == 'n')
{
anotherLoan = false;
cout<<endl<<endl;
cout<<"There were"<<loanCount<< "loans processed.";
cout<<endl<<endl;
}
}
return 0;
}

最佳答案

您是否尝试使用调试器并找到故障点?

关于c++ - 贷款计算器变更,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3329632/

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