Member Avatar for Member #114696

In earlier versions of c++, there was a function gotoxy() which could take me anywhere in the screen. Its available with borland compilers now too but not with visual c++ express. Is there any substitute for that.

Dani AI

Generated

As already noted, gotoxy() was never part of standard C/C++; it was a Borland/Turbo-C convenience. Visual C++ (Express) doesn't provide that Borland conio.h helper, so the usual substitute on Windows is the Console API: query the console buffer and call SetConsoleCursorPosition. Below is a small, practical replacement you can drop into a Visual C++ console program (it clamps coordinates so the call won't fail if the buffer is smaller than you expect):

#include <windows.h>

void gotoxy(int x, int y)
{
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    if (h == INVALID_HANDLE_VALUE) return;

    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (!GetConsoleScreenBufferInfo(h, &csbi)) return;

    SHORT maxX = (SHORT)(csbi.dwSize.X - 1);
    SHORT maxY = (SHORT)(csbi.dwSize.Y - 1);

    COORD coord;
    coord.X = (SHORT)(x < 0 ? 0 : (x > maxX ? maxX : x));
    coord.Y = (SHORT)(y < 0 ? 0 : (y > maxY ? maxY : y));

    SetConsoleCursorPosition(h, coord);
}

Notes and quick tips: COORD is zero-based (0,0 is top-left). If you are porting code that used Borland gotoxy (many Borland variants used 1-based coords), subtract 1 from each argument. If the cursor doesn't move, check that output really goes to a console (not redirected to a file) and call GetLastError() after a failed API call to diagnose why. Use GetConsoleScreenBufferInfo to fetch window vs buffer sizes if you need to work with the visible window rather than the whole buffer.

For cross-platform code use ncurses (Unix) or PDCurses (Windows), or ANSI escape sequences on modern Windows terminals. For the Borland window() behavior look into the console viewport APIs (SetConsoleWindowInfo, SetConsoleScreenBufferSize) as and hinted — those are the Win32 equivalents to create/limit text regions.

Recommended Answers

All 7 Replies

gotoxy() was NEVER EVER a standard c or c++ function, but it was a Borland-specific function that started with Turbo C. No other compiler that I know of implemented that function.

See to implement the function yourself

Member Avatar for Member #114696

Thank you! Is there a something for window() function too.

I don't know what window() does.

Member Avatar for Member #114696

thanx to all, i think i have got everything i wanted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.