Clearing the Screen

Clearing the screen on a console application appears to be related to the operating system you are using or to your compiler.

Borland C++ Builder

There are two basic ways you can clear the screen in Borland C++ Builder. The simplest technique consists of calling the system() function with the string argument "cls". Here is an example:

//---------------------------------------------------------------------------

#include <iostream.h>

#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused

int main(int argc, char* argv[])

{

    char FirstName[12], LastName[12];

    

    cout << "Enter First Name: "; cin >> FirstName;

    cout << "Enter Last Name:  "; cin >> LastName;

    

    system("cls");

    

    cout << "Full Name: " << FirstName << " " << LastName;

    

    cout << "\n\nPress any key to continue...";

    getchar();

    return 0;

}

//---------------------------------------------------------------------------

Another technique, highly known to Borland C++ Builder console programmers consists of calling the clrscr() function. The clrscr() function is part of the conio library. The clrscr() function is neither part of the C++ Standard nor part of the MS Windows operating system. Here is an example of using it:

//---------------------------------------------------------------------------

#include <iostream.h>

#include <conio.h>

#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused

int main(int argc, char* argv[])

{

    char FirstName[12], LastName[12];

    

    cout << "Enter First Name: "; cin >> FirstName;

    cout << "Enter Last Name:  "; cin >> LastName;

    

    clrscr();

    

    cout << "Full Name: " << FirstName << " " << LastName;



    cout << "\n\nPress any key to continue...";

    getchar();

    return EXIT_SUCCESS;

}

//---------------------------------------------------------------------------

MS Visual C++

To clear the screen in MSVC, first, instead of #include <iostream.h>, you must use

#include <iostream>
using namespace std;

Then, call the system() function with the string argument of "cls" where you want to clear the screen:

#include <iostream>

using namespace std;



int main()

{

    char FirstName[12], LastName[12];

    

    cout << "Enter First Name: "; cin >> FirstName;

    cout << "Enter Last Name:  "; cin >> LastName;

    

    system("cls");

    

    cout << "Full Name: " << FirstName << " " << LastName << "\n\n";

    return EXIT_SUCCESS;

}

 

Home