Solved

clear screen

Posted on 1998-05-13
8
295 Views
Last Modified: 2010-08-05
The only way that I know how to clear a the MSDOS screen while executing a program is to print out a bunch of end lines.  Is there a better way to do this that will put the cursor back at the top of the screen when done?
0
Comment
Question by:xandox
8 Comments
 
LVL 1

Expert Comment

by:vsinha
ID: 1177028
try:
#include <conio.h>
//...
clrscr();

let me know if you want further explaination
0
 
LVL 1

Expert Comment

by:vsinha
ID: 1177029
Why rejected? Doesn't work, or you didn't understand it?
0
 

Author Comment

by:xandox
ID: 1177030
I tried using the clrscr() from conio.h but I kept getting errors when I compiled saying that clrscr() was an undeclared identifier.

Am I missing anything else?

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:yonat
ID: 1177031
There is no standard way to do it. You must say what compiler you are using.
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1177032
And which operating system, too. Are you talking about a console window in Win95 or Win98 or WinNT? Or are you talking about a DOS window on 16-bit Windows?

.B ekiM


0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1177033
Under DOS, clrscr() should work.  However, in a Win32 console application there is no API to clear the screen.  It can be faked by a short routine, as explained in KB article Q99261 (reproduced below):

-------------------------------------------------------------------------
 
SUMMARY
=======
 
There is no Win32 application programming interface (API) that will clear the screen in a console application. However, it is fairly easy to write a function that will programmatically clear the screen.
 
MORE INFORMATION
================
 
The following function clears the screen:
 
void cls( HANDLE hConsole )
{
   COORD coordScreen = { 0, 0 };    /* here's where we'll home the cursor */
   BOOL bSuccess;
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
   DWORD dwConSize;                 /* number of character cells in the current buffer */
 
   /* get the number of character cells in the current buffer */
   bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
   PERR( bSuccess, "GetConsoleScreenBufferInfo" );
   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
 
   /* fill the entire screen with blanks */
   bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
      dwConSize, coordScreen, &cCharsWritten );
   PERR( bSuccess, "FillConsoleOutputCharacter" );
 
   /* get the current text attribute */
   bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
   PERR( bSuccess, "ConsoleScreenBufferInfo" );
 
   /* now set the buffer's attributes accordingly */
   bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
      dwConSize, coordScreen, &cCharsWritten );
   PERR( bSuccess, "FillConsoleOutputAttribute" );
 
   /* put the cursor at (0, 0) */
   bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );
   PERR( bSuccess, "SetConsoleCursorPosition" );
   return;
}

0
 
LVL 11

Expert Comment

by:alexo
ID: 1177034
Some additional comments:
* Don't forget to #include <wincon.h>
* PERR() is a user-supplied function or macro that is supposed to display an error.  Either remove all references to it or write it.  Otherwise the code will not compile.

A DOS window in Win16 is equivalent to a DOS application.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1177035
And the autograder hits again!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question