简体   繁体   中英

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 ?

In windows you should use windows api.

from there, use SetCursorPos() for it.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM