I believe some of you might have used Turbo C++ in the past. Turbo C++ is a 16-bit programming language and it has been outdated for a while. However, some of the functions defined in conio.h are very useful. gotoxy was used frequently in the 16-bit DOS application. So how do you use this function in the modern 32-bit or 64-bit C++ compiler?

In WinCon.h, the following XY coordinate structure is given:
typedef struct _COORD {
SHORT X;
SHORT Y;
} COORD, *PCOORD;
And the above is also included if you use windows.h. We can define the following gotoxy substitute based on Win32 API SetConsoleCursorPosition and GetStdHandle
BOOL gotoxy(const WORD x, const WORD y) {
COORD xy;
xy.X = x;
xy.Y = y;
return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy);
}
Be noted, the gotoxy does not clean the console screen, so it will set the cursor position for next printout.
/* http://HelloACM.com */
#include <iostream>
#include <windows.h>
using namespace std;
const unsigned char BLACK = 219;
BOOL gotoxy(const WORD x, const WORD y) {
COORD xy;
xy.X = x;
xy.Y = y;
return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy);
}
int main() {
gotoxy(6, 6);
cout << "HelloACM.com rocks!";
return 0;
}
So, start using 32-bit gotoxy for your console applications!
–EOF (The Ultimate Computing & Technology Blog) —
290 wordsLast Post: Simple C++ TV Screen Emulation
Next Post: Simple C++ Animation on Console Windows - Random Squares on CodePage 437