Good Afternoon,

I'm having some problems with a problem that deals with raising the power of an integer. So far I have this code.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int num, bigNum, power, count;
cout << "Enter an integer: ";
cin>>num;
cout << "What power do you want it raised to? ";
cin>>power;
bigNum = num;
while(count++ < power)
bigNum *= num;
cout << "The result is " <<bigNum<<endl;
return 0;
}

The two problems I see (besides the lack of indentation ;) ) is that you don't initialize the value of count and you have one too many iterations. A more correct code would be this:

bigNum = 1;
count = 0;
while(count++ < power)
  bigNum *= num;

Or, even better, if you try to stick with more idiomatic (or "usual") code, you should write:

int bigNum = 1;  // initialize, at the site of first use (initialization).
for(int count = 0; count < power; ++count)  // use a for-loop when you count iterations.
  bigNum *= num;

Sticking to conventional coding style is not just for esthetics, it also helps you and others to understand what you are doing, and bugs are easier to find too (because you easily see missing pieces, and formulating idiomatic code becomes a second nature).

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.