title: Raise X to the power of N. Code not working for negative exponents
body: <p>I'm working on a code which must calculate a Base to the power of an Exponent. Code seems to be correct if i input whatever Base and a non-negative Exponent.</p>
  
  <p>The code is the following:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int n;  //The Exponent
  int x;  //The Base
  double result;
  
  int main(){
      pleaseInput:
      cout &lt;&lt; "Enter Base: ";
      cin &gt;&gt; x;
      cout &lt;&lt; "Now enter Exponent: ";
      cin &gt;&gt; n;
      //If the base is 0 could be tricky...
      if(x==0){
          if(n==0){
              //0 ^ 0 = 1
              result = 1;
          }else if(n&gt;0){
              //0 ^ 3 = 0
              result = 0;
          }else if(n&lt;0){
              //0 ^ -2 is undefined.
              cout &lt;&lt; "0 to the power of a negative exponent is an undefined math operation. Please enter valid data. " &lt;&lt; endl;
              goto pleaseInput;
          }
      //If the base is other than 0...
      }else{
          //If the exponent is not 0...
          if(n!=0){
              //Make the exponent unsigned to know the amoun of iterations regardless its sign.
              unsigned int exp = (unsigned int)n;
              result = 1;
              for(int i=0;i&lt;exp;i++){
                  result *= x;    
              }
              //If the exponent was negative...
              if(n&lt;0){
                  result = 1/result;          
              }
          //If X^0....
          }else{
              result = 1;
          }
          cout &lt;&lt; x &lt;&lt;" to the power of "&lt;&lt; n &lt;&lt;" equals "&lt;&lt; result &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>Please could you guys take a look and help me find where is the mistake?</p>
  
  <p>Thanks a lot in advance!!
  Guillermo.</p>