Modern gotoxy alternative C++


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?

gotoxy Modern gotoxy alternative C++ 16 bit 32 bit c / c++ code console implementation programming languages Win32 API windows

In WinCon.h, the following XY coordinate structure is given:

1
2
3
4
typedef struct _COORD {
  SHORT X;
  SHORT Y;
} COORD, *PCOORD;
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

1
2
3
4
5
6
BOOL gotoxy(const WORD x, const WORD y) {
    COORD xy;
    xy.X = x;
    xy.Y = y;
    return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy);
}
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* 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;
}
/* 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) —

GD Star Rating
loading...
307 words
Last Post: Simple C++ TV Screen Emulation
Next Post: Simple C++ Animation on Console Windows - Random Squares on CodePage 437

The Permanent URL is: Modern gotoxy alternative C++

Leave a Reply