Clear Console using clrscr()

clrscr function is a pre-defined function present in the conio.h header file. The function upon calling clears all the data from the console, giving a blank console screen in return. The function may be called anywhere in the program, but is generally called at the start of the program to assure that the console is clear when the program has started executing. The following code displays a few lines of text then clears all those lines and displays The screen has been cleared.

Syntax:

// Header Files

main()
{
    clrscr();
    statement 2;
    statement 3;
    .
    .
}

Example:

C++




#include <conio.h>
#include <iostream>
  
int main()
{
  
    cout << "GFG!\n";
    cout << "w3wiki!\n";
  
    // Called getch() to halt the program
    // This allows us to visualize the effect produced by
    // clrscr() Upon pressing a single key the program
    // execution continues & clrscr is called
    getch();
  
    // Calling the clrscr() function
    clrscr();
  
    cout << "The screen has been cleared!";
    return 0;
}


Output:

 

After which the console screen gets cleared and the following output appears:

 

How to Clear Console in C++?

Console in C++ is the window at which the output of your program appears. Any data sent to the standard output is displayed on the console. If the console isn’t cleared while the program is executing, the next time the program is called it would output in a prefilled console screen. This hinders the readability of the output of the program. In this article, you will learn how to clear the console in C++.

Note: For clearing the console we would be making use of the clrscr function present in the conio.h library. The library may not be present in online compilers and is not a standard C++ library.

Similar Reads

1. Clear Console using clrscr()

clrscr function is a pre-defined function present in the conio.h header file. The function upon calling clears all the data from the console, giving a blank console screen in return. The function may be called anywhere in the program, but is generally called at the start of the program to assure that the console is clear when the program has started executing. The following code displays a few lines of text then clears all those lines and displays The screen has been cleared....

2. Clear Console using system(“cls”)

...