What I want to do:
The cursor is initially blinking on the top left corner of the screen:
160 characters remaining
_
When I press 'i':
159 characters remaining
i
When I press 'a':
158 characters remaining
ia
When I press 'm':
157 characters remaining
iam
and so on.
What needs to be done(According to me):
What I have tried:
I tried to clear the whole screen and write everything that was there previously back on it.
Why I am not happy with what I did:
Because it gives a jerky appearance. And the entry is not smooth.
What I need you to help me with:
Some inbuilt function or some other technique to clear only part of the screen.
My Specs:
Windows XP SP 3
IDE: Visual C++ 2010 Express
The first thing to understand is that C++ has no conception of a screen, as a standard part of the language. Standard output might be a file, a printer and cout doesn't know the difference.
The screen "device" itself, however, is typically a little smarter, and recognizes some commands. The most widely-implemented of these are '\r' - the carriage return and '\n' - the line feed. '\r' moves the cursor to the beginning of the line, and '\n' advances to the next line, but that's not fit into your needs as you've already tried.
It seems the only way forward here is to use curses (of which ncurses is only one implementation, though the standard one in Linux). It presents you with a virtual screen with various commands to update them. It then takes only the changed portions, and updates the terminal in an optimized way.
It's just an example of the typical C program using ncurses, could be worth to take 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 writes to an "imaginary" screen. It puts stuff into a buffer and updates some flags and does some other internal ncurses bookkeeping. It doesn't actually write anything to your real screen (the console window).
You can do as much printw() writing as you want to, but the stuff doesn't show up on the real screen until your program does something else to cause the "imaginary" screen buffer contents to go to the real screen.
One thing that causes the real screen to be updated from the printw() buffer is refresh() (as the source code example above does).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With