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++?
-
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
. Ahhh memories... – Trillian May 30 '09 at 18:04 -
Did you ever figure it out my man? :) – NTDLS Dec 13 '16 at 19:12
10 Answers
It used to be a function in <conio.h>, in old Borland C compilers.
It's not a C++ standard function.

- 391,730
- 64
- 469
- 606

- 176,835
- 32
- 241
- 292
-
3Actually, Microsoft C++ still has conio.h, although functions like clrscr() are not there. – Vilx- May 30 '09 at 17:44
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.
-
5That shouldn't stop people! They can just disassemble the function from the library and insert a definition in the header executing it via inline assembly! – Janus Troelsen Dec 22 '11 at 14:16
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.

- 1,870
- 1
- 13
- 27
-
3Isn't #define lowercase? Also the compiler will define WINDOWS or LINUX for you, won't it? – jmucchiello May 30 '09 at 23:03
-
I don't know if define is case sensitive. and typically that define is included via windows.h as "win32" It was just an example. – Ape-inago May 31 '09 at 17:36
-
you have to include this header file for this function
#include <conio.h>

- 19,717
- 4
- 46
- 69

- 51,913
- 37
- 138
- 191
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,117
- 1
- 11
- 20
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
}

- 19
- 2
-
2If you read the source code of `clear`, you'll find that it actually looks up the kind of terminal and issues the appropriate output for that terminal, rather than assuming that all the world is a VT100. I'll leave it as an exercise for the reader as to which should be done in a portable program. – Toby Speight Dec 14 '16 at 10:23
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.

- 8,202
- 6
- 37
- 49
On Unix-like systems you can use VT100 escape codes.
std::cout << "\033[2J" << std::flush;

- 20,267
- 14
- 135
- 196
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;

- 44,851
- 20
- 112
- 171
-
5It also assumes that the console line buffer is less than 80 lines, which is not guaranteed. – Euro Micelli May 30 '09 at 22:22
-
1Yup - but if you're worry about "clearing" a screen buffer, usually you know how many lines you're dealing with. – Eclipse May 31 '09 at 01:06
-
Mixing "endl" and "\n" seems pretty wierd. Why do that? The assumption that clearing the screen knows the number of lines seems preposterous; the "clear" or "cls" commands don't take the number of lines to be cleared, do they? The other choices will also set the cursor to the top left, while your solution will leave it at the bottom left -- and fill the user's backscroll with lots of empty lines. – MikeB Jun 01 '14 at 16:02