Continuing from the comments above, if you are outputting to a VT compatible terminal (most are), you can use ANSI Escape sequences to control clearing to end-of-line (and various other cursor movements and visibility).
A short example working with lines terminated with a carriage-return, you can use \033[K to clear to end of line. (the formal number preceding 'K' is "0K" but can be omitted due to it being the default) The other line clearing options are "1K" clear to beginning of line, and "2K" clear entire line. You can also hide and restore the cursor with similar escapes.
In your case you can handle the additional characters from previous longer lines as:
#include <iostream> #include <unistd.h> int main (void) { const char *lines[] = { "You do not ever want\r", "your favorite dog\r", "to have\r", "fleas!\r" }; int n = sizeof lines / sizeof *lines; std::cout << "\033[?25l"; /* hide cursor */ for (int i = 0; i < n; i++) { std::cout << "\033[0K" << lines[i]; /* clear to end, output line */ std::fflush (stdout); sleep (2); } std::cout << "\033[?25h\n"; /* restore cursor */ }
Compile normally and run in your terminal. If it is VT compatible it will process the escapes so you see each line printed without any of the additional characters from the preceding line interfering with the output.
32767) you can make sure the string is always padded for the correct length.cout << "\033[0k" << ...which is the ANSI escape to "Clear to end of line".