Thread: Getting a clear screen...

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question Getting a clear screen...

    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


  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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

  3. #3
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    A quick search on Google turned up this for the Win32 console (modified to become a function and to make reading easier):

    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);
    }
    Hmm. I learned something from this code too. I now know how to determine the console size.
    Last edited by Frobozz; 09-02-2004 at 02:14 PM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    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

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Finchie_88
    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

    On Windows, you can enter cls on the command line, and your console window will be cleared.

    On Linux, you can enter clear on the command line, and your console window will be cleared..

    On windows try the following program:

    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;
    }
    For linux, use "clear" instead of "cls".

    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.

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Exclamation

    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

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    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

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Actually, "cls" isn't a process but I believe that "clear" is.

    gg

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Codeplug
    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
    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.

    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

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Codeplug
    Actually, "cls" isn't a process but I believe that "clear" is.

    gg
    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).

    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

  11. #11
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Quote Originally Posted by The Brain
    There needs to be a standard on how to clear the screen that will work on all platforms.
    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).

    Code:
     void clearscreen( void ) {
       int index;
       for( index = 0; index < 5; index++ ) {
     	printf("\n\n\n\n\n");
       }
     }

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Frobozz
    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).

    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?

    Dave

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by The Brain
    there are about a billion ways to do it.. each one with it's own unique characteristics.
    Sometimes I feel like I'm in a maze of twisty little passages, all alike ...

    Dave

  14. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    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).
    It also leaves the prompt at the bottom of the terminal - not so cool

    ~/
    Last edited by kermit; 09-03-2004 at 05:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clear Screen in C++ console
    By ay_okay in forum C++ Programming
    Replies: 9
    Last Post: 12-21-2004, 04:05 AM
  2. CLear Screen Routine
    By AQWst in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2004, 08:24 PM
  3. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  4. Clear Screen Again!!!
    By trang in forum C Programming
    Replies: 3
    Last Post: 12-13-2003, 08:36 AM
  5. Yet another clear screen thread :D
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 05:14 AM