Is there a keyword and header file for getting a clear screen. For example, if you typed in a word and pressed enter, is there a function and header file that allows you to clear the screen after pressing enter...
just a thought
Is there a keyword and header file for getting a clear screen. For example, if you typed in a word and pressed enter, is there a function and header file that allows you to clear the screen after pressing enter...
just a thought
There isn't anything that is standard to do that. What OS/compiler are you using? If you're on a Windows machine, you can use system( "cls" ). If you're on a Linux machine using bash, you can do this: cout<<"\033[H\033[J";
Last edited by XSquared; 09-02-2004 at 02:04 PM.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
A quick search on Google turned up this for the Win32 console (modified to become a function and to make reading easier):
Hmm. I learned something from this code too. I now know how to determine the console size.Code:void clearscreen( void ) { COORD coordScreen = { 0, 0 }; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD dwConSize; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Handle to stdout // Gets the console resolution GetConsoleScreenBufferInfo(hConsole, &csbi); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; // Fill every cell with spaces FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); // Fill the console attributes FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); // Sets the cursor position back to the first row and column SetConsoleCursorPosition(hConsole, coordScreen); }
Last edited by Frobozz; 09-02-2004 at 02:14 PM.
Frequently asked questions
The VT100 control sequence is cool, but it should be noted that it is terminal specific. You can use the curses or ncurses library to perform a terminal independant "cls".
gg
On Windows, you can enter cls on the command line, and your console window will be cleared.Originally Posted by Finchie_88
On Linux, you can enter clear on the command line, and your console window will be cleared..
On windows try the following program:
For linux, use "clear" instead of "cls".Code:#include <stdio.h> #include <stdlib.h> int main() { printf("This line will be wiped away by the next statement.\n"); system("cls"); printf("Press \"Enter\" to exit (yes, I know that's silly): "); getchar(); return 0; }
Does anyone see any problems with this? Can you explain why I would do anything more complicated? (That's not a loaded question, and I don't mean to be confrontational, but I really want to know if there is anything Bad about this way of programmatically clearing the screen.)
Dave
Last edited by Dave Evans; 09-02-2004 at 03:44 PM.
There needs to be a standard on how to clear the screen that will work on all platforms.
It is such a commonly performed operation, yet there are about a billion ways to do it.. each one with it's own unique characteristics.
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
It's only "bad" if it has unwanted side effects - like not working, too slow, etc...
Personally, I don't like spawning a processes to accomplish something that can be done with just a few extra lines of code.
gg
Actually, "cls" isn't a process but I believe that "clear" is.
gg
Well, I certainly consider "not working" to be a bad side effect. And if I want to write a program that will run on other systems (a command-line driven text-based program whose source code can be compiled and executed under Windows or Linux, for example), then some care is obviously required, and any system() calls would be very suspect.Originally Posted by Codeplug
Compile-time switches abound in gnu code so that different platforms can be supported by a single set of source code files, but that's really hard. Make files are generated by config scripts, that use config.in files that describe and discover system characteristics, etc.
It does seem like a lot of work just to clear the stinkin' screen.
Regards,
Dave
I think this is correct: cls is a "built-in" command. In fact, I just discovered if you compile the program with system("cls"); under gcc in Windows, the program looks for an executable named cls and fails (bad side effect number 1: it doesn't work).Originally Posted by Codeplug
If you compile the program with system("clear"); under gcc in Windows and you have cygwin installed, there is an executable named clear.exe in the cywgin\bin directory, and it clears the screen. So that's a Bad thing: runs on some windows boxes but not others.
Thanks for your thoughts and feedback.
Dave
"I was born not knowing, and have only had a little time to change that, here and there."
---Richard Feynman
There is one way that is not platform dependant. Simply fill the screen with blank lines (the below saves a small amount of time by only having to loop five times).Originally Posted by The Brain
Code:void clearscreen( void ) { int index; for( index = 0; index < 5; index++ ) { printf("\n\n\n\n\n"); } }
How do you know how many lines there are?Originally Posted by Frobozz
Dave
Sometimes I feel like I'm in a maze of twisty little passages, all alike ...Originally Posted by The Brain
Dave
It also leaves the prompt at the bottom of the terminal - not so coolThere is one way that is not platform dependant. Simply fill the screen with blank lines (the below saves a small amount of time by only having to loop five times).
~/
Last edited by kermit; 09-03-2004 at 05:40 PM.