title: How to create as many nested loops as you want?
body: <p>I am trying to create a program that can take many numbers as I want in C++ language.
  Then it find what operators can make the equation true and show all correct possible operation.
  Example: If I put 3 5 15
  Then it output  3x5 = 15
  If I put 1 2 3 4 4
  Then it outputs 1+2-3+4 =4 </p>
  
  <p>The following code is my written program:
  The problem about it is that when I want to reduce the number of input or increase the number of input I need to add/reduce nested loops EVERYTIME. I want to know what is a more effective way of a more flexible nested loops or recursion method.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  
  using namespace std;
  
  
  char getOperator(int);
  double operate(int, double, double);
  
  int main() {
      double a, b, c, d, e, result;
      short noOfAnswers = 0;
      cout &lt;&lt; "Input first 5 numbers to make it equal to another 1 number.\n" &lt;&lt;
      "I'll find what are the operators needed to make 2 sides of the equation equal.\n";
      cin &gt;&gt; a &gt;&gt; b &gt;&gt; c &gt;&gt; d &gt;&gt; e &gt;&gt; result;
      int noOfOperators = 5;
      for (int i = 0; i &lt;= noOfOperators; i++) {
          double firstTwo = operate(i, a, b);
          for (int j = 0; j &lt;= noOfOperators; j++) {
              double firstThree = operate(j, firstTwo, c);
              for (int k = 0; k &lt;= noOfOperators; k++) {
                  double firstFour = operate(k, firstThree, d);
                  for (int l = 0; l &lt;= noOfOperators; l++) {
                      double firstFive = operate(l, firstFour, e);
                      if (firstFive == result) {
                          cout &lt;&lt; ++noOfAnswers &lt;&lt; ')' &lt;&lt; a &lt;&lt; getOperator(i) &lt;&lt; b &lt;&lt; getOperator(j) &lt;&lt; c
                          &lt;&lt; getOperator(k) &lt;&lt; d &lt;&lt; getOperator(l) &lt;&lt; e &lt;&lt; '=' &lt;&lt; result &lt;&lt; endl;
                      }
                  }
              }
          }
      }
      if (noOfAnswers) cout &lt;&lt; "I have found " &lt;&lt; noOfAnswers &lt;&lt; " solutions for this extremely hard problem for humanity \nin less than a second." &lt;&lt; endl;
      else cout &lt;&lt; "I cannot find any solutions to this problem.\n"
      &lt;&lt;"They're just a bunch of random numbers &amp; That is UNSOLVABLE!" &lt;&lt; endl;
      cout &lt;&lt; "Do not doubt my judgment. I am always right!" &lt;&lt; endl &lt;&lt; "(Please note that all calculations are done from the left side first.)" &lt;&lt; endl;
      return 0;
  }
  
  double operate(int iteration, double num1, double num2) {
      switch (iteration) {
          case 0: return num1+num2;
          case 1: return num1-num2;
          case 2: return num1*num2;
          case 3: return num1/num2;
          case 4: return pow(num1, num2);
          case 5: return fmod(num1, num2);
      }
      return 0;
  }
  
  char getOperator(int pos) {
      switch (pos) {
          case 0: return '+';
          case 1: return '-';
          case 2: return 'x';
          case 3: return '/';
          case 4: return '^';
          case 5: return '%';
      }
      return ' ';
  }
  </code></pre>