Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
贷款利率方案C++_C++ - Fatal编程技术网

贷款利率方案C++

贷款利率方案C++,c++,C++,我正试图编写一个程序,让用户输入金额、利率和付款数量 假设代码输出如下内容: Principal: 120000 Interest rate: 10 Number of payments: 12 No. Balance Instalment Interest Total payment ---------------------------------------------------------------- 1 120000.00

我正试图编写一个程序,让用户输入金额、利率和付款数量

假设代码输出如下内容:

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;
}

您的构造函数正在以错误的方式进行操作:

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;
}

使用调试器。在displaySchedule中的for处放置断点。看看numOfPayments的值,确认它不是零。我认为我们应该鼓励这些OP使用调试器或代码检查来发现他们的问题。调试器将比发布到StackOverflow并等待答复快得多。此外,使用检查表进行代码检查也是最快的方法。