带有变量的c++幂函数

C++ power function with variables

本文关键字:函数 c++ 变量      更新时间:2023-10-16
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, char *argv[])
{
    double amount, rate, time, interest, month;
    interest = rate/(12.0*100);
    month = (amount * rate)/1.0 -(1.0 + interest), pow(time*12);
    cout << "What is the amount of the loan? $ ";
    cin >> amount;
    cout << "What is the annual percentage rate? ";
    cin >> rate;
    cout << "How many years will you take to pay back the loan?  ";
    cin >> time;
    cout <<"n-------------------------------------------------------n";
    cout << "Amount        Annual % Interest       Years         Monthly Paymentnnn";
    cout <<amount <<"                      " <<rate <<"               " <<time << "        " <<month;
    cout <<"nn";
    system("PAUSE");
    return EXIT_SUCCESS;
}

我在这里得到一个错误,我不确定如何正确使用pow函数。根据问题的公式为:

 Monthly payment = (loan amount)(monthly interest rate) / 1.0 - (1.0 + Monthly interest rate)^-(number of months)

这是我遇到麻烦的那一行

月=(金额*率)/1.0(1.0 +利息),战俘(时间* 12);

Thanks for the help

std::pow接受如下两个参数

pow (7.0,3) 
所以你的代码应该像这样
month = (amount * rate)/1.0 - std::pow( (1.0 + interest), 12);

你的代码还有其他缺陷,比如你在设置值之前做了计算。