15
votes

I've looked everywhere for this function and cannot find the header files to make this work. It says clrscr() undeclared which brings me to the question. Is clrscr(); a function in C++?

10
One of my C teachers (who was officially a C++ teacher) used to give us a C header file defining such a method, implemented using WinAPI calls. He never took the time to explain that it wasn't part of any standard library, thus he'd always get questions from students who couldn't compile his examples at home. He was also a fan of <conio.h>. Ahhh memories... - Trillian
Did you ever figure it out my man? :) - NTDLS

10 Answers

35
votes

It used to be a function in <conio.h>, in old Borland C compilers.

It's not a C++ standard function.

12
votes

And before someone posts the usual "please email me the conio.h file" request, can I point out that this ancient Borland header file only contained the declaration of the function. You would also need the supporting Borland library, which will not be compatible with any modern C++ compilation system.

7
votes

As mentioned before, clrscr() is from turbo c++, inside conio.h

For all intents and purposes, conio.h is "non standard", and as such should be probably avoided.

I tend to use the precompiler to choose what to use for a simple clear screen, and just call the operating system's clear program.... it's smart enough to know how "tall" the screen is.

// somewhere in the program
#define WINDOWS 1

void console_clear_screen() {
  #ifdef WINDOWS
  system("cls");
  #endif
  #ifdef LINUX
  system("clear");
  #endif
}

In windows, you may want to look at the windows.h, You can interact with the windows console directly using a "handle", often noted in code as an hWin.

In linux, i've had good luck with curses/ncurses, although it is a little confusing at first.

update Calling system programs (clear.exe?)is a potential security risk - if someone is able to hijack the system call somehow thru an alternate avenue, they can force your program to do strange things. My recommendation is to dig into your platform's console api to get these things done.

3
votes

you have to include this header file for this function

#include <conio.h>
1
votes

A web search says the header file you want is 'conio.h' - I haven't tried it out, so no guarantees. Its existence may also depend on what platform you are compiling against.

1
votes

you can use the system cls command to clear the output screen..

clrscr() is from turbo c++, inside conio.h and conio.h is "non standard", and as such should be probably avoided. example

    #include<windows.h>
    main()
    {
    some code....;
    system("cls");
    some more code;
    }

its tested and works.. i use dev c++ with mingw compiler.. :)

1
votes

On Linux I always use:

void clrscr(void)
{
   fprintf(stdout, "\033[2J"); // clean screen
   fprintf(stdout, "\033[1;1H"); // move cursor to the first line
}
0
votes

The easiest way to clear the screen in real C++ is to just send out a bunch of blank lines. Of course this is assuming that stdout is directed at the screen and not a file:

for (int i = 0; i < 80; ++i)
     cout << "\n";
cout << endl;
0
votes

I used to do borland too.

Investigating curses is a good idea. It works on many unix platforms.

You might take a look at nconio at source forge.

This looks promising as well.

0
votes

On Unix-like systems you can use VT100 escape codes.

std::cout << "\033[2J" << std::flush;

See http://www.termsys.demon.co.uk/vtansi.htm