1

So given a 2D array called 'world' and the code below to output the array to the console. I want to constantly update the outputted array using a while loop to simulate movement and other actions via changes in the array values without it messily reprinting the entire thing for every update.

I would imagine the best way to do this would be to try and reset the output stream to the first line and overwrite the previous printed array each update but I am unsure on how to do this.

char world[20][20];
for (unsigned int row = 0; row < std::size(world); row++)
{
    for (unsigned int col = 0; col < std::size(world); col++) 
    {
        std::cout << world[row][col];
    }
    std::cout << std::endl;

}
Tas
  • 7,023
  • 3
  • 36
  • 51
Bantz
  • 23
  • 3

1 Answers1

1

There is a simple way to do this on one line, using \r with printf, as discussed here. However, I'm not certain this is easy if one wishes to reprint multiple lines, as you do.

A better suggestion would be to make use of something like ncurses, which seems to be designed for your purposes, and was probably used to make other programs you've seen operate this way.

Qwertycrackers
  • 626
  • 4
  • 12