Zehryo Karham
Zehryo Karham

Reputation: 95

How do I decide where to write text on the console? (ie: 97% 98% 99%....)

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?

Upvotes: 2

Views: 63

Answers (2)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

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.

Upvotes: 3

SergeyA
SergeyA

Reputation: 62613

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.

Upvotes: 3

Related Questions