Hello. Thanks in advance for helping a newbie. I am supposed to add a bottom loop (which I assume is a do while) that asks the user if he/she wishes to compute another series and as long as the answer is 'Y' or 'y' to iterate the loop until 'n' or 'N' is chosen. The loop works fine, but for some reason the answers I am supposed to get do not compute correctly. I am supposed to be getting less than 1 for every answer and after the first loop the number goes to 1.9 something. What am I doing wrong?

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

int main()
    int term, denom, finalDenom;
    double  sum = 0.0;
    char ag;
   cout << "Randy Levine \n";
   cout << endl;
            cout << "What is the final term?";
            cin    >> term; 
           while (term < 2 || term > 10)
            cout << "Invalid input. Please enter a number between 2 and 10.";
            cout << endl;
            cout << "What is the final term?";
            cin  >> term;     
            finalDenom = static_cast<int>(pow (2.0, term));
            for (denom = 2; denom <= finalDenom; denom *= 2)
   	        sum = sum + (1.0 / denom); 
            cout << "---------";
            cout << endl;
            cout << sum;
            cout << endl;
            cout << endl;
            cout << "Would you like to run another series? (Y/N): ";
            cin  >> ag;
            if (ag == 'N' || ag == 'n')
  } while (ag == 'Y' || ag == 'y');
    system ("pause");      
	return 0;
9 Years
Discussion Span
Last Post by meistrizy

Oh...I forgot. I am supposed to be testing the terms 5, 6, 8, & 10. 5 works fine but then at 6 it seems to be adding the original loop with it or something weird, whatever it is doing.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.