Open In App

How to Clear Console in C++?

Last Updated : 01 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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.

Syntax:

// Header Files

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

Example:

C++




#include <conio.h>
#include <iostream>
  
int main()
{
  
    cout << "GFG!\n";
    cout << "GeeksforGeeks!\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:

 

2. Clear Console using system(“cls”)

system is a function of stdlib.h header file. The function is used to invoke the command processor of the operating system, which is cmd.exe for Windows OS and terminal for Linux OS. We would be passing the argument cls in Windows which is a command that is used to clear the console from all previous output. Therefore, a call to the system function would be made passing the cls as an argument to clear the console. 

Note: For demonstration purposes, the system(“cls”) was located in the middle of the code. But generally, the function is called at the beginning of the code, to assure that the console is clear when the program executes. 

Syntax;

// Header Files

main()
{
    system("cls");
    statement 2;
    statement 3;
    .
    .
}

Example:

C++




#include <iostream>
#include <stdlib.h>
  
int main()
{
  
    cout << "GFG!\n";
    cout << "GeeksforGeeks!\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 & system is called
    getch();
  
    // Calling system function and passing cls as argument
    system("cls");
  
    cout << "The screen has been cleared!";
    return 0;
}


Output:

 

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

 

Note: For producing similar effect in Linux OS you have to pass clear instead of cls to the system function



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads