我需要编写需要一个函数来计算power
在C++功能功率C++
我不明白为什么我下面的代码是错误的。
ex。 :基地:2的指数3 - >结果:4.48498e-306 如果我使用 “INT” 的结果是 - > 2
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
double power(double base, double exponent);
int main()
{
double base, exponent, power;
cout << "Enter a base " << endl;
cin >> base;
cout << "Enter an exponent" << endl;
cin >> exponent;
cout << "The result is ': "<< power << endl;
return 0;
}
double power(double base, double exponent)
{
int i;
double s=1;
for (i = 0; i < exponent; ++i)
s *= base;
return s;
}
我在做什么错?
我无法重现此错误;我得到8. – Beta
嗯...你永远不会打电话功能... – bolov
除此之外,使exponente int和一切都很好。 – bolov