EL-Mehdi Loukach
EL-Mehdi Loukach

Reputation: 772

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 ?

Upvotes: 3

Views: 25926

Answers (3)

Sergey
Sergey

Reputation: 96

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!");
}

Upvotes: 5

Denis Sablukov
Denis Sablukov

Reputation: 3690

Look at ncurses library for creating text-based user interfaces. It works fine with Linux and with Windows under Cygwin/MinGW.

Upvotes: 2

Lorence Hernandez
Lorence Hernandez

Reputation: 1189

In windows you should use windows api.

from there, use SetCursorPos() for it.

Upvotes: 2

Related Questions