title: Adding a Series of Fraction to the nth Power and Finding the Sum - C++
body: <p>I need to write a program that adds 1/2^1 + 1/2^2 + 1/2^3......and gives the user the option to enter the nth term they would like it to go to. (between 2-10) </p>
  
  <p>It needs to display the fractions (1/2 + 1/4 + 1/8....) then find out their sum and display at the end. (1/2 + 1/4 + 1/8 + 1/16 + 1/32 = .96875)</p>
  
  <p>I'm missing something crucial in my code and I'm not sure what I'm doing wrong. I'm getting the fractions to show up multiple times before the program adds them together. </p>
  
  <pre><code>// This program displays a series of terms and computes its sum.
  #include &lt;iostream&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  int main()
  {    
      int denom,              // Denominator of a particular term    
          finalTerm,    
          nthTerm;                 // The nth term
      double sum = 0.0;       // Accumulator that adds up all terms in the series
  
     // Calculate and display the sum of the fractions. 
     cout &lt;&lt; "\nWhat should the final term be? (Enter a number between 2 and 10)";
     cin &gt;&gt; finalTerm;
  
     if (finalTerm &gt;= 2 &amp;&amp; finalTerm &lt;= 10) 
     {   
          for (nthTerm = 1; nthTerm &lt;= finalTerm; nthTerm++)
          {   
              denom = 2;
              while (denom &lt;= pow(2,finalTerm))
              {   
                  cout &lt;&lt; "1/" &lt;&lt; denom;
                  denom *= 2;
  
                  if (nthTerm &lt; finalTerm)
                      cout &lt;&lt; " + ";
                  sum += pow(denom,-1);
              }   
          }   
          cout &lt;&lt; " = " &lt;&lt; sum;
     }   
     else
         cout &lt;&lt; "Please rerun the program and enter a valid number.";
  
      cin.ignore();
      cin.get();
  
      return 0;
  }  
  </code></pre>