hey how to clear the screen in c++ i m using dev c++. clrscr()doesnot work

Dani AI

Generated

Quick summary for : clrscr() is not part of standard C++. The thread already shows several approaches (Win32 API, system("cls"), printing newlines, Borland conio.h), and correctly points out that clearing the screen is often unnecessary. Below are compact, practical alternatives that work in real projects and notes on when to use each.

A simple, widely-supported method for POSIX terminals (and many modern terminals) is to emit the ANSI escape sequence that clears the screen and moves the cursor home:

#include <iostream>

int main() {
    std::cout << "\x1B[2J\x1B[H"; // clear screen + move cursor to top-left
    return 0;
}

This is portable across Linux and macOS. It may not affect scrollback and will print raw bytes if output is redirected to a file.

On Windows consoles, recent Windows 10+ builds support ANSI sequences if virtual-terminal processing is enabled. The following helper toggles that mode (check return values in real code):

#ifdef _WIN32
#include <windows.h>

bool enableVirtualTerminal() {
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hOut == INVALID_HANDLE_VALUE) return false;
    DWORD mode = 0;
    if (!GetConsoleMode(hOut, &mode)) return false;
    return SetConsoleMode(hOut, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0;
}
#endif

For full-screen or robust cursor control (windows of text, panels, forms), use a dedicated library: ncurses on Unix-like systems or PDCurses on Windows. Example usage pattern:

#include <ncurses.h>

int main() {
    initscr();
    clear();
    refresh();
    getch();
    endwin();
    return 0;
}

Practical guidance: avoid system("cls") or system("clear") where possible (security and performance). Printing many newlines is a cheap hack but does not reset cursor position or attributes. For small updates (progress, counters) prefer overwriting a line with \r or using cursor-movement sequences instead of clearing the whole screen. Choose the method that matches portability needs: ANSI for simple cross-platform output, ncurses/PDCurses for interactive UIs, and native Win32 APIs for Windows-only tools (as demonstrated).

Recommended Answers

All 7 Replies

from MSDN:

#include <windows.h> 

void clrscr(void)
{
    COORD                       coordScreen = { 0, 0 };
    DWORD                       cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO  csbi;
    DWORD                       dwConSize;
    HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    FillConsoleOutputCharacter(hConsole, TEXT(' '), 
                               dwConSize, coordScreen, &cCharsWritten);
    GetConsoleScreenBufferInfo(hConsole, &csbi);
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
                               dwConSize, coordScreen, &cCharsWritten);
    SetConsoleCursorPosition(hConsole, coordScreen);
}

Windows only obviously

hey how to clear the screen in c++ i m using dev c++. clrscr()doesnot work

You could try;

#include <windows.h>

System("CLS"); // Not a good way to do it however, but gets the job done

void ClearScreen()
    {
    cout << string( 100, '\n' );
    } 

And now for the obligatory question: why do you think you need to clear the screen? There are only a handful of reasons where it makes sense outside of a GUI environment, and quite a few reasons why it's a bad idea in a textual environment (the kind of environment where clrscr() accomplishes anything meaningful).

Here are a couple of good ones:

  1. Clearing the screen also clears the output from previous programs. The implcit assumption that your program owns the output window is not safe. Nothing kills a user's interest in a program you wrote like anti-social behavior.

  2. Clearing the screen is inherently non-portable. If you can get away with an interface that doesn't require a non-portable solution, the task of porting your code can be simplified. And there's a side benefit of not having to hear purists (such as yours truly) rail on about lack of portability when you discuss your code in places like Daniweb. ;)

commented: thumbs up from another "purist" +13

Umm... u sure u added the header file ? I know it sounds rather stupid even mentioning it, but it is a pretty rookie mistake.. !! Add the header file and try clrscr() , like this below :

#include<conio.h>

void main()
   { 
      clrscr();
      getch();
    }

Dev-C++ uses MinGW as the back end (by default), which ultimately means you're using the GCC compiler. GCC doesn't support clrscr() at all even though MinGW's version of GCC does support getch() after a fashion. Typically, you can only find clrscr() on Borland compilers, so even if the OP includes <conio.h>, it still won't work.

Oh... alright.. Gotcha !! Thnks for the edit :)

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.