title: C++ - Overwrite Multiple Lines that were Previously Output to Console
body: <p>If I have a grid of characters displayed in the console, is there any practical way to re-write those multiple lines in order to output an altered grid on those same lines of the console.</p>
  
  <p>e.g, I would like this code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int main() {
          for (int i=0; i&lt;5; i++) {
                  for (int j=0; j&lt;5; j++) {
                          cout &lt;&lt; "-";
                  }
                  cout &lt;&lt; endl;
          }
  
          getchar();
  
          for (int i=0; i&lt;5; i++) {
                  cout &lt;&lt; '\r';
                  for (int j=0; j&lt;5; j++) {
                          cout &lt;&lt; "x";
                  }
                  cout.flush();
          }
  
          return 0;
  }
  </code></pre>
  
  <p>to output:</p>
  
  <pre><code>-----
  -----
  -----
  -----
  -----
  </code></pre>
  
  <p>then, upon user input, overwrite that with;</p>
  
  <pre><code>xxxxx
  xxxxx
  xxxxx
  xxxxx
  xxxxx
  </code></pre>
  
  <p>I see other examples of people displaying loading bar type displays by outputting '\r' to re-write a <em>single</em> line, but I'm not sure if there is any straightforward way to accomplish that for <em>multiple</em> lines?</p>
  
  <p>I'm using MinGW.</p>
  
  <p><strong>One Solution:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;windows.h&gt;
  
  using namespace std;
  
  void gotoxy( int column, int line )
    {
    COORD coord;
    coord.X = column;
    coord.Y = line;
    SetConsoleCursorPosition(
      GetStdHandle( STD_OUTPUT_HANDLE ),
      coord
      );
    }
  
  int main() {
          for (int i=0; i&lt;5; i++) {
                  for (int j=0; j&lt;5; j++) {
                          cout &lt;&lt; "-";
                  }
                  cout &lt;&lt; endl;
          }
  
          getchar();
  
          gotoxy(0,0);
  
          for (int i=0; i&lt;5; i++) {
                  for (int j=0; j&lt;5; j++) {
                          cout &lt;&lt; "x";
                  }
                  cout &lt;&lt; endl;
          }
  
          return 0;
  }
  </code></pre>