The first thing to understand is that C ++ has no concept of the screen as a standard part of the language. The standard output may be a file, the printer and cout do not know the difference.
The device screen itself is usually a little smarter and recognizes some commands. The most widely implemented of them are "\ r" - carriage return and "\ n" - line. "\ r" moves the cursor to the beginning of the line, and "\ n" moves to the next line, but this does not meet your needs, as you have already tried.
It seems that the only way forward is to use curses (of which ncurses is only one implementation, although standard on Linux). It presents you with a virtual screen with various commands for updating them. Then it accepts only the changed parts and optimizes the terminal.
This is just an example of a typical C program using ncurses, it might be worth a look:
#include <ncurses.h> int main() { int ch; initscr(); /* Start curses mode */ raw(); /* Line buffering disabled */ keypad(stdscr, TRUE); /* We get F1, F2 etc.. */ noecho(); /* Don't echo() while we do getch */ printw("Type any character to see it in bold\n"); ch = getch(); /* If raw() hadn't been called * we have to press enter before it * gets to the program */ printw("The pressed key is "); attron(A_BOLD); printw("%c", ch); attroff(A_BOLD); refresh(); /* Print it on to the real screen */ getch(); /* Wait for user input */ endwin(); /* End curses mode */ return 0; }
The printw () function is written to the "imaginary" screen. It places the material in the buffer and updates some flags, and also performs some other internal operations with ncurses. In fact, it does not write anything on your real screen (console window).
You can do as many printw () as you want, but the material is not displayed on the real screen until your program does something else to bring the "imaginary" contents of the screen buffer to the real screen.
One thing that causes the real screen to be updated from the printw () buffer is updated () (as the source code example shows).
source share