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.
e.g, I would like this code:
#include <iostream>
using namespace std;
int main() {
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
cout << "-";
}
cout << endl;
}
getchar();
for (int i=0; i<5; i++) {
cout << '\r';
for (int j=0; j<5; j++) {
cout << "x";
}
cout.flush();
}
return 0;
}
to output:
-----
-----
-----
-----
-----
then, upon user input, overwrite that with;
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
I see other examples of people displaying loading bar type displays by outputting '\r' to re-write a single line, but I'm not sure if there is any straightforward way to accomplish that for multiple lines?
I'm using MinGW.
One Solution:
#include <iostream>
#include <stdio.h>
#include <windows.h>
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<5; i++) {
for (int j=0; j<5; j++) {
cout << "-";
}
cout << endl;
}
getchar();
gotoxy(0,0);
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
cout << "x";
}
cout << endl;
}
return 0;
}