Click to See Complete Forum and Search --> : Functions


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?

bryce777
03-28-2002, 01:53 PM
You are incrementing x in the loop declaration, and again inside the loop.

You should probably take the one in the loop out.

f15e
03-28-2002, 01:59 PM
Damn, what the hell was I thinking? Apparantly I wasn't! Sometimes the obvious is invisible. Must be some magic act. haha
Thanks Bryce777

bryce777
03-28-2002, 02:17 PM
Originally posted by f15e
Damn, what the hell was I thinking? Apparantly I wasn't! Sometimes the obvious is invisible. Must be some magic act. haha
Thanks Bryce777

Yeah, I have written a million of those things, and once in a while I still do something retarded like put a semicolon after the ).

ouch!