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?
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?
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).
There are a bunch of other functions in the same part of the MSDN library. Some of them may be useful too.
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);
}
This was on stackoverflow...
`#include <stdio.h>
// ESC-H, ESC-J (I remember using this sequence on VTs)
#define clear() printf("\033[H\033[J")
//ESC-BRACK-column;row (same here, used on terminals on an early intranet)
#define gotoxy(x,y) printf("\033[%d;%dH", (y), (x))
int main(void)
{
clear();
gotoxy(23, 12);
printf("x");
gotoxy(1, 24);
return 0;
}`
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 );
}
For Windows: #include<windows.h>
#define cursor(x, y) SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), (COORD){x, y})
For Linux:
#define cursor(x,y) printf("\033[%d;%dH", x, y)
I figured out this to set the cursor.
#include <iostream>
void setPos(std::ostream& _os, const std::streamsize& _x, const std::streamsize& _y)
{
char tmp = _os.fill();
if(_y>0) {
_os.fill('\n');
_os.width(_y);
_os << '\n';
}
if(_x>0) {
_os.fill(' ');
_os.width(_x);
_os << ' ';
}
_os.flush();
_os.fill(tmp);
}
int main(int argc, char **argv)
{
setPos(std::cout, 5, 5);
std::cout << "foo" << std::endl;
return 0;
}
To do more you'll need assumptions on the resolution or a lib like ncurses.