Please help me how to use clrscr(); and gotoxy(); functions in other C Compilers?

Dani AI

Generated

was right: clrscr() and gotoxy() come from Borland's old conio.h (Turbo C / DOS era) and are not part of standard C or C++. That is why code using them fails on modern compilers. There are three practical approaches depending on your target platform and how portable you need the program to be.

For simple terminal programs where the terminal supports ANSI escape sequences (Linux, macOS, and most modern Windows consoles), you can move the cursor and clear the screen with escape codes. Example (C-style, works in C or C++ on ANSI-capable terminals):

#include <stdio.h>

void gotoxy(int x, int y) {
    /* ANSI uses row;col and is 1-based */
    printf("\x1b[%d;%dH", y, x);
}

void clrscr(void) {
    printf("\x1b[2J\x1b[H");
    fflush(stdout);
}

If you need robust, portable terminal control (windowing, input, colors), use a terminal library such as ncurses on Unix-like systems or PDCurses on Windows. Minimal ncurses pattern:

#include <ncurses.h>

int main(void) {
    initscr();
    clear();
    mvprintw(4, 10, "Name: John");
    refresh();
    getch();
    endwin();
    return 0;
}

For native Windows console apps, showed the Win32 API approach (useful when you need exact Windows behavior). Note that newer Windows 10+ consoles also honor ANSI sequences if virtual terminal processing is enabled. Quick troubleshooting notes: ANSI uses (row, col) while many gotoxy(x,y) implementations use (col, row); terminals start counting at 1; flush output after escape sequences; check that stdout is a TTY (not redirected) before moving the cursor; avoid system() calls in production code for portability and security. For new projects, prefer ncurses/PDCurses or ANSI sequences over nonstandard conio.h calls.

Recommended Answers

All 6 Replies

Please read this thread. posted a couple days ago

gotoxy is a borland-specific function which was never implemented by any other compiler. If you want to use it with other compilers you will have to write your own gotoxy() function using operating system specific api calls.

if you are using visual C++ it is very simple to write your own functions.

// function definition -- requires windows.h

void gotoxy(int x, int y)
{
HANDLE hConsoleOutput;
COORD dwCursorPosition;
cout.flush();
dwCursorPosition.X = x;
dwCursorPosition.Y = y;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
}

// function definition -- requires process.h
void clrscr()
{
system("cls");
}

How if I'm using Dev-C++

>// function definition -- requires process.h
The only header needed for that function is <cstdlib>.

in DEV C++


#include <windows.h>

void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_H ANDLE), coord);
}

the clrscr function should work

write a name and birthplace and birthday in gotoxy turbo c++

commented: Nope -1
commented: huh? +0
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.