贷款和利率计划c ++

时间:2014-10-29 19:20:04

标签: c++

我正在尝试编写一个程序,以便用户输入金额,利率和付款次数。

代码假设输出如下内容:

Principal: 120000
Interest rate: 10
Number of payments: 12

No.        Balance     Instalment       Interest  Total payment
----------------------------------------------------------------
  1      120000.00       10000.00        1000.00       11000.00
  2      110000.00       10000.00         916.67       10916.67
  3      100000.00       10000.00         833.33       10833.33
  4       90000.00       10000.00         750.00       10750.00
  5       80000.00       10000.00         666.67       10666.67
  6       70000.00       10000.00         583.33       10583.33
  7       60000.00       10000.00         500.00       10500.00
  8       50000.00       10000.00         416.67       10416.67
  9       40000.00       10000.00         333.33       10333.33
 10       30000.00       10000.00         250.00       10250.00
 11       20000.00       10000.00         166.67       10166.67
 12       10000.00       10000.00          83.33       10083.33
----------------------------------------------------------------
                        120000.00        6500.00      126500.00

但是我写的代码输出如下:

Number of payments: 12
No.        Balance     Instalment       Interest  Total payment
----------------------------------------------------------------
----------------------------------------------------------------
000Program ended with exit code: 0

这是我到目前为止的代码。

    #include <iostream>

using namespace std;

class Loan
{
public:
    Loan(double upphaed, double vextir, int afborganir);
    void displaySchedule();

private:
    void pay(int i);
    double payments();
    double interest(int i);
    double Balance(int i);

    double sumOfInstalments;
    double sumOfInterest;
    double total;
    double principal;
    double interestRate;
    int numOfPayments;
};

Loan::Loan(double upphaed, double vextir, int afborganir)
{
    upphaed = principal;
    vextir = interestRate;
    afborganir = numOfPayments;
    sumOfInstalments = 0.0;
    sumOfInterest = 0.0;
    total = 0.0;

}

void Loan::pay(int i)
{
    sumOfInstalments += payments();
    sumOfInterest += interest(i);
    total += payments() + interest(i);
}

double Loan::payments()
{
    return principal / numOfPayments;
}

double Loan::interest(int i)
{
    return Balance(i) * (interestRate / 12) / 100;
}

double Loan::Balance(int i)
{
    return principal - (i - 1) * payments();
}

void readData(double& principal, double& interestRate, int& numOfPayments)
{
    cout << "Principal: ";
    cin >> principal;
    cout << "Interest rate: ";
    cin >> interestRate;
    cout << "Number of payments: ";
    cin >> numOfPayments;
}



void Loan::displaySchedule()
{
    cout << "No.        Balance     Instalment       Interest  Total payment" << endl;
    cout << "----------------------------------------------------------------" << endl;

    for (int i = 1; i <= numOfPayments; i++)
    {
        cout << i << "        " << Balance(i) << "     " << payments() << "     " << interest(i) << "     " << (payments() + interest(i));
        pay(i);
    }
    cout << "----------------------------------------------------------------" << endl;
    cout << sumOfInstalments << sumOfInterest << total;

}
int main()
{
    double amount, interestRate;
    int numPayments;

    readData(amount, interestRate, numPayments);
    Loan myLoan(amount, interestRate, numPayments);
    myLoan.displaySchedule();

    return 0;
}

1 个答案:

答案 0 :(得分:3)

你的构造函数正在以错误的方式处理:

Loan::Loan(double upphaed, double vextir, int afborganir)
{
    upphaed = principal;
    vextir = interestRate;
    afborganir = numOfPayments;
    sumOfInstalments = 0.0;
    sumOfInterest = 0.0;
    total = 0.0;
}

而不是:

Loan::Loan(double upphaed, double vextir, int afborganir)
{
    principal == upphaed;
    interestRate = vextir;
    numOfPayments = afborganir;
    sumOfInstalments = 0.0;
    sumOfInterest = 0.0;
    total = 0.0;
}