title: How can I stop a For loop in C++ when two conditions are met?
body: <p>I am trying to get my for loop to stop running once two conditions are met. the <code>n</code> counter is not giving me an issue. I basically would like the loop to stop running once <code>totalSum</code> is equal to itself (or within a certain margin of itself) after the loop has started. <em>Im a beginner at coding and new to this site so I apologize in advance</em>. for testing purposes on anyones behalf, i'll include the code in its entirety.</p>
  
  <p><strong>I will provide more info from here on</strong>. my code runs as it should, although during the looping, the values being calculated become to large and I get something saying <code>nan</code> or <code>inf</code>. after printing out <code>totalSum</code> and such. I see that I get the value (correct sine calculation) repeated once or twice, but after that the number keeps being incremented or decremented by the loop . Once my loop finally ends after 100 times I get the aforementioned <code>nan</code>/<code>inf</code>.</p>
  
  <pre><code>long double sine(double x)                                       
  {
  long double totalSum = 0.0;
  
  for(int n = 0; n &lt; 100; ++n)
  {
      int plusOrMinusSign = pow(-1, n);
      long double num = pow(x,(2 * n + 1));
      long double den = factorialtest(2 * n + 1);
      totalSum += (plusOrMinusSign * num / den);
      cout &lt;&lt;plusOrMinusSign&lt;&lt;" "&lt;&lt; x &lt;&lt; "^" &lt;&lt;(2*n+1) &lt;&lt;"/"&lt;&lt; (2 * n + 1)&lt;&lt;"! = " &lt;&lt; totalSum &lt;&lt;endl; //testing purposes
  }
  return totalSum;
  }
  </code></pre>
  
  <p>whole program below</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;    //used for pow indicated in comment with "^"
  #include &lt;iomanip&gt;
  using namespace std;
  
  long int factorialtest(int x) //calculates factorial
  {
  long int factorialNum = 1;
  for(int count = 1; count &lt;= x; count++)
      factorialNum = factorialNum * count;
  return factorialNum;
  }
  
  long double sine(double x)                                             
  {
  long double totalSum = 0.0;
  
  for(int n = 0; n &lt; 100; ++n)
  {
      int plusOrMinusSign = pow(-1, n);
      long double num = pow(x,(2 * n + 1));
      long double den = factorialtest(2 * n + 1);
      totalSum += (plusOrMinusSign * num / den);
      cout &lt;&lt;plusOrMinusSign&lt;&lt;" "&lt;&lt; x &lt;&lt; "^" &lt;&lt;(2*n+1) &lt;&lt;"/"&lt;&lt; (2 * n + 1)&lt;&lt;"! = " &lt;&lt; totalSum &lt;&lt;endl; //testing purposes
  }
  return totalSum;
  }
  
  double cosine(double x)                     //returns cos of x
  {
  double totalSum = 0;
  
  for(int n = 0; n &lt; 100; n = n + 2)
  {
      double num = pow(x,(n));            // ^ used pow here, does the power of x to the n value above
      double den = factorialtest(n);      //used my own factorial program here, multiplies n by factorial of n
      totalSum = totalSum + ( num / den);
  }
  return totalSum = 0;
  }
  
  double tangent(double x) {      //returns tangent
  return sine(x) / cosine(x);
  }
  
  double secant(double x) {       //returns secant
  return 1 / cosine(x);
  }
  
  double cosecant(double x) {     //returns cosecant
  return 1 / sine(x);
  }
  
  double  cotangent(double x) {   //returns cotangent
  return 1 / tangent(x);
  }
  
  int main() {
  
  double x;
  
  cout &lt;&lt; "Input number to find sine: ";
  cin &gt;&gt; x;
  cout &lt;&lt; "Sine of " &lt;&lt; x &lt;&lt; " is " &lt;&lt; sine(x) &lt;&lt; endl;
  
  
  }
  </code></pre>
  
  <p>Program calculating radians btw. Example of what is happening below using 1 for x. "Sine(1)" </p>
  
  <pre><code>-1 1^63/63! = 0.841471
  1 1^65/65! = 0.841471
  -1 1^67/67! = -inf
  </code></pre>
  
  <p>After a certain point it gives me that. Thank you for your time.</p>