Thread: Clear Screen in C++ console

  1. #1
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69

    Clear Screen in C++ console

    I want to clear the screen, but system("cls") doesn't work with windows Xp. Can anyone tell me of a library with a function to clear the screen or another method?

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I think system("clrscr") works for one of the systems but im prolly wrong (newbie)

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    You might want to check your code again - I am running win xp myself and calling system("cls"); works great...

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Smile Clear the screen?

    If you mean in an MSDOS window, I would do it manually. Try this:
    Code:
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    If that's not what you're looking for, sorry. Also, it may not look real nice, but I am pretty new to C++, and at least it works

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    41
    I can vouch for the system("cls") command. I have it in one of my programs and it works great on Windows XP.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Use of the system function is another one of those things that will recieve nasty comments on this forum. Basically the problem is that it depends too heavily on external resources that are easily tampered with. Your code won't work on non-Windows systems, and even on Windows systems, it's easy to remove the CLS program.

    In this case, it's not such a terrible sin, but keep this in mind.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    check in your conio.h file...it's still not very portable......clearing your screen in general isn't really all that 'portable' either....what if someone didn't want their screen cleared when they ran your program? what if another program called your program? it's certainly not nice to erase someone elses hard work
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    oh yea....read the FAQ!

    but, while the thread is here, i will take the oppurtunity to ask a related question....
    how do programs typically clear the screen the polite way in *nix. for example, vim or nano.
    they appearantly erase the whole, but somehow the console retains all the text that it did before it called nano or vim.....my first thought is that they store a "copy" of it and rewrite it on exit....but then i thought, what if vim or nano crashed? (of course, this has never happened )
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  9. #9
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Taken from Alvaro on the cpp-home forums:

    void clrscr(){
    std::cout << "\33[2J\33[1;1H" << std::flush;
    }
    It works on most platforms (Windows included).

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I have a 6 part tutorial on console programming here. Clearing the screen is covered in part 2. If you are doing a lot of console programming, there is a lot of material of potential use there.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clear Screen Again!!!
    By trang in forum C Programming
    Replies: 3
    Last Post: 12-13-2003, 08:36 AM
  2. Yet another clear screen thread :D
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 05:14 AM
  3. How to clear the screen?
    By Zopyrus in forum C++ Programming
    Replies: 8
    Last Post: 11-07-2003, 10:20 PM
  4. Clear Screen
    By KneeGrow in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2003, 10:17 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM