title: Rewriting a program without using goto statements
body: <p>I had to program a similar problem using goto statements.  Now we are asked to rewrite our code without using goto statement. I don't know how to begin doing this program.  I paste in the previous program code using the goto.  </p>
  
  <pre><code>// Eight Queens problem using one dimesional array and goto statement
  
  #include "stdafx.h"
  #include &lt;iostream&gt;
  using namespace std;
  
  
  int main()
  {
      int q[8];
      q[0] = 0;
      int c = 0;
      int count = 0;
  
  NC: //cout  &lt;&lt; "Next column\n" &lt;&lt; "Column = " &lt;&lt; c &lt;&lt; endl;
      c++;
      if (c == 8) goto print;
      q[c] = -1;
  
  NR: //cout &lt;&lt; "Next row\n" &lt;&lt; "Row = " &lt;&lt; q[c] &lt;&lt; "\nColumn = " &lt;&lt; c &lt;&lt; endl;
      q[c]++;
      if (q[c] == 8) goto backtrack; 
      for(int i = 0; i &lt; c; i++){
          if(q[i] == q[c] || abs(q[c] - q[i]) == (c - i))
              goto NR;
      }
      goto NC;
  
  backtrack:
      //cout &lt;&lt; "Backtrack" &lt;&lt; endl;
      //cout &lt;&lt;"Column = " &lt;&lt; c &lt;&lt; endl;
      c--;
      if(c == -1) return 0;
      goto NR;
  
  print:
      //cout &lt;&lt; "print" &lt;&lt; endl;
      ++count;
      cout &lt;&lt; count &lt;&lt; endl;
      for(int i = 0; i &lt;= 7; i++){
              cout &lt;&lt; q[i];
      }
      cout &lt;&lt; endl;
      goto backtrack;
  
  
      return 0;
  }
  </code></pre>
  
  <p>This program is a hint the professor posted for the class to use.</p>
  
  <pre><code> #include &lt;iostream&gt;
      #include&lt;cstdlib&gt;
      #include &lt;cmath&gt;
      using namespace std;
  
      bool ok(int q[], int col){
      if the configuration is “bad” return false;
      else
      return true;
      }
  
      void backtrack(int &amp;col){
      col--;
      if(col==-1) exit(1);
      }
  
      void print(int q[]){
      static int count =0;
      print the array q
      }
  
      int main(){
      int q[8]; q[0]=0;
      int c=1;
      // from_backtrack keeps track if we need to reset the row to the
      // top of the current colum or not.
  
      bool from_backtrack=false;
      // The outer loop keeps looking for solutions
      // The program terminates from function backtrack
      // when we are forced to backtack into column -1
      while(1){
      while(c&lt;8){ //this loop goes across columns
      // if we just returned from backtrack, use current value of row
      // otherwise get ready to start at the top of this column
      if(!from_backtrack) // we did not just return from backtrack
      Code goes here
      from_backtrack=false;
      while(q[c]&lt;8){ // place queen in this column
      q[c]++;
      // if row=8, there is no valid square in this column
      // so backtrack and continue the loop in the previous column
      Code goes here
      //if this position is ok, place the queen
      // and move on (break) to the next column,
      // otherwise keep looking in this column
      Code goes here
      }
      c++; // placed ok, move to the next column
      }
      // one complete solution found, print it.
      print(q); // board completed, print it out
      backtrack(c);
      from_backtrack=true;
      }
      }
  </code></pre>
  
  <p>And this is an attempted I've made to completed the program</p>
  
  <pre><code>// NoGoto.cpp.cpp : Defines the entry point for the console application.
  //
  
  #include "stdafx.h"
  #include &lt;iostream&gt;
  using namespace std;
  
  
  
  bool attack(int q[], int col){
      if(q[i] == q[c] || abs(q[c] - q[i]) == (c - i)) return false;
      else
          return true;
  } // Attack
  
  void backtrack(int &amp; col){
      col--;
      if(col == -1) exit(1);
  } // Backtrack
  
  void print(int q[]){
      static int count = 0;
      ++count;
      cout &lt;&lt; count &lt;&lt; endl;
      for(int i = 0; i &lt; 8; i++)
          cout &lt;&lt; q[i];
      cout &lt;&lt; endl;
  }
  int main()
  {
      int q[8];
      q[0] = 0;
      int c = 1;
  
      bool from_backtrack = false;
  
      while(1){
          while(c &lt; 8){ // this loops across columns
  
              if(!from_backtrack)
                  attack(q[c],c)
  
              from_backtrack = false;
  
              while(q[c] &lt; 8){ // place queen in this column
                  q[c]++;
  
      return 0;
  }
  </code></pre>
  
  <p>"I'm having problem writing the code. How [can I] call each function to make it correctly find the solutions?"</p>