How to position the ... cursor?

I have problem in my console app.
For example I want to draw a calculator and then the user enter his numbers in a square in that cal. so I need to take the cursor there but have no idea how!
So if anyone know how can I do this Plz Help!
Even so if it is possible with bat file commands tell me so I can make it by system() function ... help me ... I'm in short of time ...
Include the following code in your program.

#include <windows.h> 
void gotoxy( short x, short y ) 
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ; 
    COORD position = { x, y } ; 
    SetConsoleCursorPosition( hStdout, position ) ; 

When you call the function it should look like this:

gotoxy(60,20); // 60 is the COLUMN and 20 is the LINE 

EDIT: PS: The console has 20 lines and 80 columns.

Hope I could help.
~ Raul ~
Last edited on
Thanks alot Raul!
Yeah it worked.
Would U plz explain it for me?
What exactly does this gotoxy do?
May I use it in Linux?
Sorry for late reply.

So, firstly, you have to include the <windows.h> header file in order to be able to use the specific console functions and variables: "
HANDLE, COORD, GetStdHandle, SetConsoleCursorPosition
Now, what your own defined function is doing is actually having you get the control over the "console" via HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;. Now that you gained control over it, you declare your cursor position as a COORD structure with two parameters X, and Y (where X represents the column and Y the line). SetConsoleCursorPosition( hStdout, position ) ; is the function that actually gives you the full control on the cursor position. It takes two arguments: the first is the variable which holds control on the console window and the second is the coordinate x,y pair.

Actually, gotoxy(); is a function that exists in C, but it wasn't implemented in the official C++ libraries. Thus, using the already existing implemented functions you can create your own gotoxy(); function.

For further details about this function I recommend reading on Microsoft Windows' website.
As about using it in Linux, I am sorry but I am quite sure it won't work. I only program over Windows and I have been using this function. Maybe asking on the UNIX/Linux section of the forum and posting a link to this topic might get you to the answers you are looking for.

Glad I could help.
~ Raul ~
Last edited on
Thanks alot Raul. I mean e.. really thanks. I O U
No problem mate. You can write me PM's whenever you need help.

~ Raul ~
Topic archived. No new replies allowed.