Move text cursor to particular screen coordinate?

38,902

Solution 1

Use SetConsoleCursorPosition.

There are a bunch of other functions in the same part of the MSDN library. Some of them may be useful too.

Solution 2

Neither C nor C++ have any notion of a screen or console; they only see streams of bytes, which have no inherent display characteristics. There are a number of third-party APIs like ncurses to help you do that.

If you want a quick-n-dirty solution and the terminal you're working with understands ANSI escape sequences, then you can do things like

printf("\033[%d;%dH", row, col);

to move the cursor to a specific row and column (where the top left corner is {1,1}). You'd be better off using ncurses, though (or the equivalent for your platform).

Solution 3

In case you are talking about ncurses library, the function you are after is move (row, column).

Solution 4

I use a really simple method. You don't overly need to know what a HANDLE is unless you're really diving into console applications, a COORD object is in the windows.h standard library and has two member data intergers X and Y. 0,0 is the top left corner and Y increases to go down the screen. You can use this command and just continue to use std::cout<< to print whatever you need.

#include <windows.h>

int main(void){
//initialize objects for cursor manipulation
HANDLE hStdout;
COORD destCoord;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

//position cursor at start of window
destCoord.X = 0;
destCoord.Y = 0;
SetConsoleCursorPosition(hStdout, destCoord);
}

Solution 5

You can use this to set the cursor to specific coordinates on the screen, and then simply use cout<< or printf statement to print anything on the console:

#include <iostream>
#include <windows.h>

using namespace std;

void set_cursor(int,int);

int main()
{
     int x=0 , y=0;
     set_cursor(x,y);
     cout<<"Mohammad Usman Sajid";

     return 0;
}

void set_cursor(int x = 0 , int y = 0)
{
    HANDLE handle;
    COORD coordinates;
    handle = GetStdHandle(STD_OUTPUT_HANDLE);
    coordinates.X = x;
    coordinates.Y = y;
    SetConsoleCursorPosition ( handle , coordinates );
}
Share:
38,902
user1232138
Author by

user1232138

Updated on July 09, 2022

Comments

  • user1232138
    user1232138 almost 2 years

    How can I set the cursor at the desired location on the console in C or C++?

    I remember a function called gotoxy(x,y), but I think its deprecated. Is there any alternative?

  • Algoman
    Algoman over 6 years
    Unlike gotoxy, your function can only move the cursor forward. Also, the position is relative to the current cursor-position in your solution - if the cursor is at (x,y), then a call to setPos(a,b) moves to ((y==0?x:0)+a,y+b) - so the new x-coordinate depends on the parameter you pass for y.
  • lupz
    lupz over 6 years
    Exactly. It is shifting the output by a desired offset. In fact it does this by producing output. If I recall correctly I used something like this for table-like output formatting.
  • John P
    John P over 6 years
    If you print \r and then move forward, you can advance to an arbitrary column, right? You would only be able to control the Y coordinate if you knew the width of the terminal, since the output would naturally wrap at intervals of that. If it works at all, I would start with a \r or \n to make sure you're starting at (0,0), and make sure nothing prints at the same time.