C++/seting position of cursor on desired location at the screen in c++
Expert: Ralph McArdell - 11/25/2007
QuestionAoa.i am a student and i have to do ana assignment in which i have to set the postion of cursor on the screen where a specific letter is.so i want to know that who i can do this?
AnswerThe C and C++ language standards have _no_ support for such functions.
You do not mention what operating system you are trying to do this on nor what possible libraries you have available to you and have been using. I presume you must have covered this on your course - check out your course notes, and if necessary refer to whatever library documentation you have available for any such library you might be using. You should use any such library in preference to the techniques described here as this would presumably be what your tutor expects.
The reason the operating system is important is that it determines what system API (Application Programming Interface) functions or libraries are likely to be available - and it is an operating system dependent function or third party library you need to use in this case. Again: use any such library you have been told to use on your course in preference to those described here.
For example, if you are using a UN*X or Linux platform there is a library called curses (or ncurses) that performs terminal-based operations. It originally was intended for use with real terminals which tended to required various control code sequences to perform the sort of operations you are after. Today of course many people will just use an xterm session under the X window system on these platforms. You use the function move(x,y) or wmove function to move the current position, or you can use functions with the mv prefix (mv for move) to move the position while performing an operation, such as mvaddch (move to position and add a character to the output window) and mvaddchstr (move to position and add a character to the output window). For more information read a good set of documentation, such as the ncurses programming how-to (which may be installed somewhere on you system or might be on a Linux distribution disc), or can be found at
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ on cusrses/ncurses. Another site that appears to have useful information is
http://web.cs.mun.ca/~rod/ncurses/ncurses.html and
http://web.cs.mun.ca/~rod/ncurses/ncurses.html#curses Here is a simple example:
#include <curses.h>
int main()
{
initscr();
move(10,10); // Move to line 10, character 10
printw("Hello\n");
refresh();
sleep(3); // Keep displayed output on screen for 3 seconds
endwin();
}
We can combine the move and printw operations using the mvprintw function thus:
#include <curses.h>
int main()
{
initscr();
mvprintw(10,10, "Hello\n");
refresh();
sleep(3); // Keep displayed output on screen for 3 seconds
endwin();
}
You should build the above example using a command line something like:
g++ example.cpp -o example -lncurses
Which is for building using the GNU C++ compiler. If this does not work then it may be that ncurses (or curses) is not installed on you system and you should check to see if a version is available. For example if you are using a Linux distribution then there may be ncurses packages that need to be installed - I found with my current Ubuntu installation I had to install the ncurses development package to ensure the header files and documentation were available rather than just the shared libraries - which were installed already for use by other installed applications.
If however you are using a Microsoft Windows based system then there are MS Windows ports of this library - see for example PDCurses at
http://gnuwin32.sourceforge.net/packages/pdcurses.htm
If you are using a Microsoft Windows platform and not concerned about doing it the UN*X way then you can use the Win32 console API functions directly. The function you require is SetConsoleCursorPosition, however before you use this function you have to obtain a handle to the screen buffer of the console you are using - for a simple program which is running in a pre-created console window you can obtain such a handle using the GetStdHandle function and passing it STD_OUTPUT_HANDLE. Note you should (as usual with MS Windows API programming) include windows.h.
I could explain these functions in detail; however MS freely provide this information in the MSDN library - a version of which should be included on CD with MS Visual C++ (version 6 or later). Alternatively you can find the information at
http://msdn2.microsoft.com/ - try starting here:
http://msdn2.microsoft.com/library/ms682010.aspx
Here is a sample program:
#include <iostream>
#include <windows.h>
int main()
{
HANDLE hConsole( GetStdHandle(STD_OUTPUT_HANDLE) );
COORD pos;
pos.X = 10;
pos.Y = 10;
SetConsoleCursorPosition( hConsole, pos );
std::cout << "Hello\n";
return 0; // not required for standard C++
// but some (broken) compilers like VC6 will need it
}
You should link with Kernel32.lib.
Oh, you will need an implementation of the Microsoft Windows Platform SDK to use the Windows API functions such as those above as it contains the header files and import libraries to use the Windows API. This is definitely supplied with the Microsoft compilers (or can be downloaded for use with it for the free Express edition), but may not be available for other compilers for Windows. The MinGW Windows port of the GNU C++ compiler (used by DevC++) has its own versions of Windows SDK headers and libraries - see
http://www.mingw.org/ for more information.