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

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.