【问题标题】:Loan calculator alteration贷款计算器更改
【发布时间】:2010-07-25 14:55:02
【问题描述】:

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

问题来了:

编写一个程序,让用户以年数为单位输入贷款金额和贷款期限,并显示从 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++ c++builder


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多