Thread: Clear screen?

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > 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.

  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
    28,412
    Did you read the FAQ that Salem linked to?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,412
    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
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,195
    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...

  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 01: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,229
    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 04: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, 08: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, 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