Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control a cursor position in c++ console application?

I'm supposed to create a console application for school project and it's about Sudoku Game, so the thing is i don't find any struggle with the algorithm, but i was wondering if i could draw the full Sodoku table with c++ and make empty squares as "data" input place so the user can move the cursor using the arrow keys to the specific number's place to fill it with the appropriate number.

Is there a method to do it this way ?

like image 355
EL-Mehdi Loukach Avatar asked Oct 27 '25 17:10

EL-Mehdi Loukach


1 Answers

It depends on your OS/Compiler. For example, in VC++ you can use this and example can be found here.

#include <windows.h>
int main(){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {3, 6};
SetConsoleCursorPosition(hConsole, pos);
WriteConsole(hConsole, "Hello", 5, NULL, NULL);
return 0;
}

If you want to do it in Linux with g++ compiler, you can use special libraries such as curses or write your own implementation(will be a bit difficult). Such for just placing the cursor at the required position, you can use this:

void gotoxy(int x,int y)    
{
    printf("%c[%d;%df",0x1B,y,x);
}
void clrscr(void)
{
    system("clear");
}
int main() {    
    int x=10, y=20;
    clrscr();
    gotoxy(x,y);
    printf("Hello World!");
}
like image 152
Sergey Avatar answered Oct 30 '25 07:10

Sergey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!