#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 Payment\n\n\n";
cout <<amount <<" " <<rate <<" " <<time << " " <<month;
cout <<"\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
我在这里遇到错误,不确定如何正确使用 pow 函数。根据问题的公式为:
Monthly payment = (loan amount)(monthly interest rate) / 1.0 - (1.0 + Monthly interest rate)^-(number of months)
这是我遇到问题的行
month = (amount * rate)/1.0 -(1.0 + interest), pow(time*12);
感谢帮助
最佳答案
std::pow
像这样接受两个参数
pow (7.0,3)
所以你的代码应该更像这样
month = (amount * rate)/1.0 - std::pow( (1.0 + interest), 12);
您的代码还有其他缺陷,比如您在设置值之前进行了计算。
关于带变量的 C++ 幂函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15320350/