Home > database >  Why is my pow function returning 0 when it's being called as a variable?
Why is my pow function returning 0 when it's being called as a variable?

Time:04-16

I'm new to coding and have been learning on youtube and one of the functions im learning is the pow function. When i call the function in a cout directly, it outputs the correct value, but when i use a variable to call it, it outputs 0 as the value. Am i missing a step in the declaration of the function?

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

 }

CodePudding user response:

Call double power = pow(base, exponent); after base, exponent are assigned values

cin >> exponent;
double power = pow(base, exponent);
cout << power << endl;
  • Related