为什么我的 pow 函数在作为变量被调用时返回 0?
Why is my pow function returning 0 when it's being called as a variable?
我是编码新手,一直在 youtube 上学习,我正在学习的函数之一是 pow 函数。当我直接调用 cout 中的函数时,它输出正确的值,但是当我使用变量调用它时,它输出 0 作为值。我是否遗漏了函数声明中的一个步骤?
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
using std::cin;
int main()
{
int base, exponent;
double power = pow(base, exponent);
cout << "What base do you have? " << endl;
cin >> base;
cout << "What exponent do you have? " << endl;
cin >> exponent;
cout << power << endl;
return 0;
}
调用double power = pow(base, exponent);
后base, exponent
被赋值
cin >> exponent;
double power = pow(base, exponent);
cout << power << endl;
我是编码新手,一直在 youtube 上学习,我正在学习的函数之一是 pow 函数。当我直接调用 cout 中的函数时,它输出正确的值,但是当我使用变量调用它时,它输出 0 作为值。我是否遗漏了函数声明中的一个步骤?
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
using std::cin;
int main()
{
int base, exponent;
double power = pow(base, exponent);
cout << "What base do you have? " << endl;
cin >> base;
cout << "What exponent do you have? " << endl;
cin >> exponent;
cout << power << endl;
return 0;
}
调用double power = pow(base, exponent);
后base, exponent
被赋值
cin >> exponent;
double power = pow(base, exponent);
cout << power << endl;