#include <iostream>
usingnamespace std;
int main()
{
int x;
int y;
int result = x^y;
cout<<"Please enter a number(base): \n";
cin>>x;
cout<<"Now enter the exponent please: \n";
cin>>y;
cout<<x<<" raised to the power of "<<y<<" is "<<result;
return 0;
}
Please enter a number(base):
5
Now enter the exponent please:
2
5 raised to the power of 2 is 4196208
I need help correcting this code to display the correct answer.
line 11: int result = x^y; x and y are uninitialised. Therefore their values could be equal to anything.
The following result = x^y;, only works once, not for the entire lifespan of the program. If you want result = x^y all the time, you need to make this a function. Or calculate it after you initialise x and y.
Furthermore the XOR operator '^' here isn't doing what you intend (see : http://www.cplusplus.com/doc/boolean/) instead you can use the pow() function (include cmath).
#include <iostream>
#include <cmath>
usingnamespace std;
int result(int num, int p) { return (pow(num, p)); }
int main()
{
int x;
int y;
cout << "Please enter a number(base): \n";
cin >> x;
cout << "Now enter the exponent please: \n";
cin >> y;
cout << x << " raised to the power of " << y << " is " << result(x, y);
return 0;
}
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int x;
int y;
int result;
cout << "Please enter a number(base): \n";
cin >> x;
cout << "Now enter the exponent please: \n";
cin >> y;
result = pow(x, y);
cout << x << " raised to the power of " << y << " is " << result;
return 0;
}