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;

}

Recommended Answers

All 12 Replies

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

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....?

Gotta show yer code...

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...

Oh right! Duh, because anything times zero equals zero.

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.

for (...)
    product = product * base;

What would be a suitable product (factor) to begin with?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.