Return to Topic Menu | Computer Science Main Page |  | Terms of Use

Clearing the Screen, Flush, and Pause





Clearing the Screen:         system("CLS");

When the screen is cleared in Visual C++, the cursor is moved to the upper left corner of the screen.  To clear the screen in Visual C++, utilize the code:  system("CLS");
The standard library header file <stdlib.h> is needed.

Note:  If you wish to clear the screen after a cout statement, you will need to "flush" the iostream.  

  system ("CLS");
cout<< "Hello"<<flush; //flush forces the printing to the screen before it clears
system ("CLS");
cout<< "Good-Bye"<<endl;
Flushing the Output Stream:

You will discover that it will be necessary on occasions to "clear the output stream".  All information going "out" to the screen goes through the output stream.  (Thus the header named <iostream.h> - input/output stream.)

To force the information to go to the screen immediately (and not wait for more information to enter the stream before outputting to the screen), we will be using  flush.

The correct syntax will be:

cout << "Flushing the output stream." << flush;

If you are using a clear screen command (system ("CLS");) in the middle of a program and it does not appear to be working ...
flush the cout statements which precede the clear screen command.






Pausing the screen until user hits a key: 

Note:   This command will "pause" the screen and display the message "Press any key to continue . . . " as it waits for a keystroke. If you wish to pause the screen after a cout statement, you will need to "flush" the iostream. (Requires header file <stdlib.h>)

  cout<< "Hello"<<endl<<flush; //flush forces the printing to the screen before it pauses
system ("PAUSE"); //pause displays a message and waits for the user to hit a key
cout<< "Good-Bye"<<endl;


//This program will clear the screen and print a message.

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

int main(void)
     system ("CLS");
     cout << "\tHi, there!\n\n";
     cout << "\nComputers are";
     cout << "great!\n";
     return 0;

Let's see what the output from this program will look like.  The screen display is shown in the table below.  The cells of the table are used to represent the coordinates of the screen.
 Refer to your grid sheet.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
                H i ,   t h e r e !  
C o m p u t e r s   a r e g r e a t !

 Of course, there are more than 19 columns across your screen (there are 80). Notice the following things:

  • the placement of the word Hi in the 9th column after the \t (tab) was executed.

  • the number of lines that were skipped in relation to the number of \n commands.

  • the lack of any empty spacing between the word are and great.