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

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

Edited 6 Years Ago by Nick Evan: n/a

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

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. ;)

Edited 4 Years Ago by deceptikon

Comments
thumbs up from another "purist"

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();
    }

Edited 4 Years Ago by deceptikon: Fixed the code block

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.

This article has been dead for over six months. Start a new discussion instead.