|
-
Catfish
Functions
#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?
-
You are incrementing x in the loop declaration, and again inside the loop.
You should probably take the one in the loop out.
I'm half Scottish and half French.
I surrender to alcohol.
-
Catfish
function
Damn, what the hell was I thinking? Apparantly I wasn't! Sometimes the obvious is invisible. Must be some magic act. haha
Thanks Bryce777
-
Re: function
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!
I'm half Scottish and half French.
I surrender to alcohol.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|