I'd like to be able to tell
cout where to write some specific text so that, for instance, I can show an increasing percentage that will be "updated" by overwriting it every time it changes.
Something like a
Progress: 97% that becomes
Progress: 98% and then
Progress: 99% all in the same place, possibly without re-processing the whole screen output every time.
I remember QBasic (from the very old days) having a method specific for this purpose, but it seems I cant find anything more "modern" in C++. Wherever I go, I see people mentioning
Curses or other "cursed" libraries that, I'm afraid, would bloat my code with unwanted features and possible holes and malfunctions.
Or worse, they suggest to just flood the output stream with tens or hundreds of
\n to "hide the dust under the carpet".
Another idea that has just come to my mind would be to build some kind of array that stores outputs to be then processed in a looped
cout-ish function so that all I have to do is to change the part of the array that I need to, somehow purge the current screen content and re-output it all over again with the modifications. Except this would still require Windows' cursed
system library to clear the screen or just
Curses; or a
\n machine-gun function to hide the former output.
Any more elegant and/or C++ standard based viable solution?
Any more elegant and C++ standard based viable solution?
Basically no, because as mentioned C++ standard has no notion of your terminals capabilities.
There's the standard escape character literal
'\b' for putting a backspace (position the cursor back within the current line) though. This should work with most available terminal types.
For the simple code as shown most people would use a simple console output followed by \r. This will move cursor to the left position on the same line on most terminals, so you can print 'on top' of already printed line. This is definitely not C++ standard, but I find it elegant enough.
對於如圖所示的簡單代碼,大多數人會使用簡單的控制台輸出,然后是\ r \ n。這會將光標移動到大多數終端上同一行的左側位置,因此您可以打印已打印行的“在頂部”。這絕對不是C ++標准,但我發現它足夠優雅。