I'm working on an assignment where I have to write my own raisePower function to take a base and a power and compute the product. The assignment states that the function needs to return a long but all input/output values should be ints. The code compiles and runs but does not produce the correct product. Any help or suggestions would be appreciated. Thanks.

#include <iostream>
using namespace std;


long raisePower(int base, int power)
{
	long product;

	for (int i = 1; i <= power; i++)
	{
		product = base * base;
	}
	
	return product;
}

int main() 
{

	int base;
        int power;
	int product;

	cout << "Enter an Integer for the Base: ";
	cin >> base; 

	cout << "Enter an Integer for the Power: ";
	cin >> power;

        product = raisePower(base, power);

	cout << base << " Raised to the Power of " << power << " is " << product << "\n";
	
	return 0;

}

Edited 4 Years Ago by NickPatton: n/a

product = base * base; <-- look here.

I changed it to product = product * base. The compiler started chirping about how long product wasn't initialized to zero so I made it: long product = 0 Still not getting the correct output though. I enter 2 as the base and 3 as the power and get 0 as the product. Am I moving in the right direction or....?

This would be a good place to use a debug output. Print out the value of power, base, product, and i inside of your "for" loop. This should make it obvious what is going on.

Also, you declare "product" in tow different places in your code. You didn't say which one you initialized to 0. Are you sure you initialized the correct occurrence?

Putting debug output in.....

Here's my code as of now though:

#include <iostream>
using namespace std;


long raisePower(int base, int power)
{
	long product = 0;

	for (int i = 1; i <= power; i++)
	{
		product = product * base;
	}
	
	return product;
}

int main() 
{

	int base;
    int power;
	int product;

	cout << "Enter an Integer for the Base: ";
	cin >> base; 

	cout << "Enter an Integer for the Power: ";
	cin >> power;

    product = raisePower(base, power);

	cout << base << " Raised to the Power of " << power << " is " << product << "\n";
	
	return 0;

}

OK, I've put in the debug statements and learned that my raisePower function is getting the correct parameters for base and power but product is still equal to zero. I believe the issue is in my for loop.

Here's my latest code:

#include <iostream>
using namespace std;


long raisePower(int base, int power)
{
	long product = 0;

	cout << "TEST Base: " << base << "\n";
	cout << "TEST Power: " << power << "\n";
	
	for (int i = 1; i <= power; i++)
	{
		product = product * base;
	}
	cout << "TEST Product: " << product << "\n";
	return product;
}

int main() 
{

	int base;
    int power;
	int product;

	cout << "Enter an Integer for the Base: ";
	cin >> base; 

	cout << "Enter an Integer for the Power: ";
	cin >> power;

    product = raisePower(base, power);

	cout << base << " Raised to the Power of " << power << " is " << product << "\n";
	
	return 0;

}
long product = 0;
//...
product = product * base;

So product becomes...

OK, I'm no longer initializing product to zero but when I run the code, product equals garbage and the compiler gives an error.

I got it! Initialize product to 1! Here's the final working code:

#include <iostream>
using namespace std;


long raisePower(int base, int power)
{
	long product = 1;

	
	for (int i = 1; i <= power; i++)
	{
		product = product * base;
	}
	
	return product;
}

int main() 
{

	int base;
    int power;
	int product;

	cout << "Enter an Integer for the Base: ";
	cin >> base; 

	cout << "Enter an Integer for the Power: ";
	cin >> power;

    product = raisePower(base, power);

	cout << base << " Raised to the Power of " << power << " is " << product << "\n";
	
	return 0;

}

Thanks for your help Caligulaminus.

Edited 4 Years Ago by NickPatton: n/a

This question has already been answered. Start a new discussion instead.