Clear screen?

This is a discussion on Clear screen? within the C++ Programming forums, part of the General Programming Boards category; Is there a clear screen command in C++ because I need one and I have yet to find it....

  1. #1
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20

    Clear screen?

    Is there a clear screen command in C++ because I need one and I have yet to find it.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Do you mean in console?

    system("cls");

    That should do it.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    good lord...


    Why is everything so much harder in C++???

    I'm using dev-C++ the most recent version, and I'm making a standard windows program, how do I clear the screen without changing anything, only clearing what has been outputted to the screen

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > Why is everything so much harder in C++???
    Compared to what?

    I dunno what you've been using, but you have to understand that C++ doesn't give a damn about your OS or the hardware on which it runs.

    The word "screen" doesn't appear in the C++ standard, so consequently, there is no STANDARD way to "clear a screen". If (and I stress, IF) there is indeed a screen on your implementation, then how to clear is it part of the implementation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    Quote Originally Posted by Salem View Post
    > Why is everything so much harder in C++???
    Compared to what?

    I dunno what you've been using, but you have to understand that C++ doesn't give a damn about your OS or the hardware on which it runs.

    The word "screen" doesn't appear in the C++ standard, so consequently, there is no STANDARD way to "clear a screen". If (and I stress, IF) there is indeed a screen on your implementation, then how to clear is it part of the implementation.
    Will it help people to know that my programming background is in basic? I'm looking for a kind of clear screen like the Cls command in Basic

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Did you read the FAQ that Salem linked to?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    Quote Originally Posted by laserlight View Post
    Did you read the FAQ that Salem linked to?
    Yes I did, I understand that C++ has no screen function because of the fact that it is multiplatform (understated)

    I'm still a bit lost

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,647
    Ah, but the FAQ answer also presents a number of options. The first one seems pretty simple, assuming that you know the length of the screen: "Write newline characters until everything has scrolled off of the screen".

    The fifth one (use system(), e.g., system("cls") or system("clear")) might be the one you are looking for, but of course it has its own disadvantages.

    If all else fails, you could always go for the fourth option
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by The7thCrest View Post
    good lord...


    Why is everything so much harder in C++???

    I'm using dev-C++ the most recent version, and I'm making a standard windows program, how do I clear the screen without changing anything, only clearing what has been outputted to the screen
    Its not...
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I don't understand why using system to clear a screen is remotely popular. Let's look at this example:

    Code:
    $ cd /tmp
    $ cat test.cpp 
    #include <iostream>
    #include <cstdlib>
    
    int main()
    {
      // Because this program needs root
      setuid(0);
    
      // Show us all how good I am
      system("clear");
      std::cout << "Look, mom, I cleared the screen!!!" << std::endl;
    }
    Now, since this program needs root, we set the setuid bit:
    Code:
    $ make test
    g++     test.cpp   -o test
    $ sudo chown root:root test
    $ sudo chmod 4755 test
    Yes, it works as expected. Now...
    Code:
    $ cat clear
    #!/bin/bash
    head -n 1 /etc/shadow
    $ chmod 755 clear
    $ export PATH=/tmp:$PATH
    $ ./test
    root:!:13843:0:99999:7:::
    Look, mom, I cleared the screen!!!
    This shows a bit on the security and portability of such a thing. And I wont even start talking about the speed...

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If you have windows just copy/paste Option's 5 clear_screen function and you are done. Or inlcude conio.h and use clrscr() as noted int the FAQ (non-standard). If you have Linux use Option's 4 method.

    P.S.: I would like an Option 7 in the FAQ that is like Option 6 but for Linux, even though it would require installing some library

  13. #13
    Registered User
    Join Date
    Jan 2009
    Location
    Salisbury N.C.
    Posts
    20
    I think I need a better understanding of how C++ before I figure this out can someone volunteer to add me to AIM and try and explain somewhat how to do this and why I have to do it this way?



    EDIT: I have gotten it to work with
    Code:
    system("cls");
    , but what is the purpose of the different methods? What are their uses, I'm sorry normally I'm a quick learner, but that FAQ confuses the **** outta me
    Last edited by The7thCrest; 01-03-2009 at 12:27 AM.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    903
    Well each method has its advantages / disadvantages. I don't really see what more could be said about them.

    Have you read the speed / security disadvantages of using system() (and not just system("cls") ) ?

    I would go with the newline method. Just print a bunch of newlines and be done with it.

  15. #15
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I don't quite understand the point on security, either. Even if someone places a "cls.exe" in the program's working directory, the system one should still take precedence, since we are not doing "./cls.exe"? (does Windows actually search the current directory before the rest of $PATH?

    If the cls.exe in system32 (or where ever it is) is replaced... I think there is a bigger issue to worry about than how secure this C++ program is.
    [edit]
    IMHO the best option is to use "system("cls");", unless you want to clear the console 60 times per second or something like that.

    Sure, it's not portable (and I'm a Linux guy), but just changing the "cls" to "clear" would make it work on UNIX. If he is just starting to learn C++, there are certainly more important things to spend time on than this little detail?

    It's also much simpler than all other solutions.

    So I say just make sure you know the limitations and shortcomings of this method, and use it anyways =).
    [/edit]
    Last edited by cyberfish; 01-03-2009 at 03:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CLear Screen Routine
    By AQWst in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2004, 07:24 PM
  2. 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
  3. Getting a clear screen...
    By Finchie_88 in forum C++ Programming
    Replies: 13
    Last Post: 09-03-2004, 05:38 PM
  4. Clear Screen Again!!!
    By trang in forum C Programming
    Replies: 3
    Last Post: 12-13-2003, 07:36 AM
  5. Yet another clear screen thread :D
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 04:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21