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;
      
  do
  { 
            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')
            break;
              
  } while (ag == 'Y' || ag == 'y');
         
    system ("pause");      
	return 0;
}

Recommended Answers

All 3 Replies

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.

Reset num back to 0..

while(blah..blah)
(
   num=0;
   //...
   num=num+(1.0/denom);
)

Cool! Thanks cikara21!

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.