You are here:

C++/seting position of cursor on desired location at the screen in c++

Advertisement


Question
Aoa.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?


Answer
The 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.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.