f15e
03-28-2002, 01:43 PM
#include<iostream>
using namespace std;
void PowerFunc(int, int);
void main()
{
int x, y;
cout << "Enter two ints: ";
cin >> x >> y;
PowerFunc(x,y);
}
//************************************************************ ****
void PowerFunc(int a, int b)
{
int sum = 1;
cout << "The number " << a << " is to be raised "
<< " the power of " << b <<endl;
for(int x = 1; x <= b; x++)
{
sum *= a;
x++;
}
//cout << x <<endl;
cout << "The answer is " << sum << endl;
}
//************************************************************ ****
When two ints are entered and passed to the PowerFunc, it gives me the two numbers I entered but it doesn't give me the correct answer. It shows with the cout<<x<<endl; statement that it goes through the loop 5 times and not 4. Here is a sample output:
Enter two ints: 2 4
The number 2 is to be raised to the power of 4
The answer is 4
What am I doing wrong?
using namespace std;
void PowerFunc(int, int);
void main()
{
int x, y;
cout << "Enter two ints: ";
cin >> x >> y;
PowerFunc(x,y);
}
//************************************************************ ****
void PowerFunc(int a, int b)
{
int sum = 1;
cout << "The number " << a << " is to be raised "
<< " the power of " << b <<endl;
for(int x = 1; x <= b; x++)
{
sum *= a;
x++;
}
//cout << x <<endl;
cout << "The answer is " << sum << endl;
}
//************************************************************ ****
When two ints are entered and passed to the PowerFunc, it gives me the two numbers I entered but it doesn't give me the correct answer. It shows with the cout<<x<<endl; statement that it goes through the loop 5 times and not 4. Here is a sample output:
Enter two ints: 2 4
The number 2 is to be raised to the power of 4
The answer is 4
What am I doing wrong?