Funtions and powers

I have written a program to have the user enter 2 values and put the first one as the base and the second as the exponent. It comes out the right answer but it still says error. I'm not sure why when the answer is right it outputs error instead of verified. It only verifies certain combinations of numbers

#include <iostream>
#include <math.h>
#include <stdlib.h>

using namespace std;

float add (float a, float b)
{
float c = a + b;
return c;
}

float subtract (float a, float b)
{
float c = a - b;
return c;
}
float multiply (float a, float b)
{
float c = a * b;
return c;
}

float divide (float a, float b)
{
float c = (a/b);
return c;
}

float pow ( int a, int b)
{
float c = a^b;
return c;
}

int main ()
{

float x;
float y;

cout << "Please enter a number " << endl;
cin >> x;
cout << " Please enter an integer " << endl;
cin >> y;

float power = pow (x,y);
cout << "The answer is " << power << endl;
if (power == pow (x,y))
{
cout << "Verified" << endl;
}
else if ( power != pow (x,y))
{
cout << "Error" << endl;
}



return 0;
}
Last edited on
1
2
3
4
5
float pow ( int a, int b)
{
  float c = a^b; // ^ means xor in c++, use pow from cmath
  return c;
}


1
2
3
4
float x;
float y;
//...
float power = pow (x,y); // here you are calling float pow ( float, float ) -which is from cmath- not your overload 


 
if (power == pow (x,y)) // floats are not precise so you can't check for equality 

Topic archived. No new replies allowed.