TOPIC 6.2.1b
Clearing the Screen & Printing


Clearing the Screen

As you have read clearing the screen and printing vary depending on the compiler and operating system you are using. In most DOS C++ environments their exists a header file called conio.h, that includes the the function clrscr(). The function is used in the function clear_the_screen() as follows:

#include <conio.h>

void clear_the_screen()
{
    clrscr();
}

Calling this function will instantly clear the screen, and move the cursor to the top of the screen.

In compilers such as Microsoft Visual C++, the clrscr function does not work. In compilers that do not support clrscr, try the following. This method has been tested on the Microsoft compiler.

First, include stdlib.h at the top of your program. Then, just insert the following call whenever you want to clear the screen.

system("CLS");

The program below is an example of how to use this method of clearing the screen.

#include <iostream.h>
#include<stdlib.h>

int main()
{
 cout << "Text before the clear screen." << endl;

 system("CLS");

 cout << "Text following the clear screen." << endl;

 return 0;
}

Printing to a Printer

To print to a local printer in a DOS programing environment, the fstream.h header file can be used to create a stream of type ofstream. When using the ofsteams open function, replace the filename with the a port name, such as LPT1. Once this has been done, output to the printer can be accomplished in the same manner as printing information to a file. The following example is a short program to illistrate how to send data to a printer.

#include <iostream.h>
#include <fstream.h>

void main()
{
  ofstream aPrinter;
  aPrinter.open("LPT1", ios::out);

  aPrinter<<"Look at me, I'm printing.";

  aPrinter<<"\f";

  aPrinter.close();
}

The aPrinter<<"\f"; statement sends a signal to the printer to eject the paper, and the aPrinter.close(); statement is used to close the data stream, just like when outputing to a file.


Additional information available on the Knowlton & Associates' Internet Distance Education Web site at http://www.studio-cplus.com