This short article describes the method of clearing the console display of all text and positioning the text cursor in the home location (the upper-left corner).
Before becoming too comfortable doing this kind of thing blithely, make sure you read and understand
Types and purposes of console applications.
- - - - - - - - - - - - - - - - The Simple Answer - - - - - - - - - - - - - - - - |
While simple, it really is a
Bad Thing. See
Why system() is evil for more.
1 2 3 4 5 6 7
|
#ifdef __cplusplus__
#include <cstdlib>
#else
#include <stdlib.h>
#endif
if (system("CLS")) system("clear");
| |
- - - - - - - - - - - - - - - - The Standard Way - - - - - - - - - - - - - - - - |
Pathetic, but passing, and usually correct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifdef __cplusplus__
#include <iostream>
#include <string>
void ClearScreen()
{
cout << string( 100, '\n' );
}
#else
#include <stdio.h>
void ClearScreen()
{
int n;
for (n = 0; n < 10; n++)
printf( "\n\n\n\n\n\n\n\n\n\n" );
}
#endif
| |
This works, of course, just by putting a hundred newlines to the display. Over a poorly-buffered network connection, this might be
s l o w. Alas.
- - - - - - - - - - - - - - - - Using NCurses - - - - - - - - - - - - - - - - |
The Curses library is designed for working with the console. Advantages: it is cross-platform. Disadvantages: it doesn't interact well with the standard streams. In other words, you shouldn't mix
printf(), etc or
cout, etc with curses. Use one or the other, not both.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <curses.h>
int main()
{
char users_name[ 100 ];
initscr();
(void)echo();
addstr( "What is your name> " );
refresh();
getnstr( users_name, sizeof( users_name ) - 1 );
clear();
printw( "Greetings and salutations %s!\n", users_name );
refresh();
printw( "\n\n\nPress ENTER to quit." );
refresh();
getnstr( users_name, sizeof( users_name ) - 1 );
endwin();
return 0;
}
| |
Again, if all you want to do is clear the screen on occasion then using Curses is complete overkill.
- - - - - - - - - - - - - - - - Using <conio.h> - - - - - - - - - - - - - - - - |
This library is severely deprecated, but it is so popular that some form of it exists on most 80x86 hardware compilers --almost always on Windows compilers, and there exist Linux versions too. However, given the choice, use NCurses instead...
The caveat is that it is non-standard, meaning that the actual functions it provides vary a lot and they don't always behave just right. Hence, for anything other than Windows programs it is also a sub-optimal solution. See
Using <conio.h> for more.
If undaunted, then here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <conio.h>
#include <stdio.h>
#include <string.h>
int main()
{
char users_name[ 100 ];
printf( "What is your name> " );
fgets( users_name, sizeof( users_name ), stdin );
*strchr( users_name, '\n' ) = '\0';
clrscr();
printf( "Greetings and salutations %s!\n", users_name );
printf( "\n\n\nPress ENTER to quit." );
fgets( users_name, sizeof( users_name ), stdin );
return 0;
}
| |
Now, the enterprising among you may have tried to compile this. If it worked for you, you're lucky. If not, then you have learned firsthand the shortcomings of the <conio.h> library. Alas.