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):
- Need to clear the only the first three characters of the screen.
- Update the newly pressed key on the screen
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
If you want to manage the complete screen,
curses
is the way to go. Otherwise, you can do a lot just using escape sequences; see http://en.wikipedia.org/wiki/ANSI_escape_code, for example. (Historically, such sequences varied from one terminal to the next, and curses was originally a way of working around this. Today, the ANSI escape codes are pretty universal for console windows under a windowing system, being used by both Windows console window and xterm.)In addition to encapsulating the actual sequences,
curses
supports character oriented input, with or without echo. This is more difficult to do without curses, and is still very unportable.Win32
console doesn't support escape sequences. You can use Console API.Tiny example that clears first 3 characters at (0, 0) from your console
If you don't like
Console API
and wish to usencurses
analog, see there.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:
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).