system("cls")

This is a discussion on system("cls") within the C++ Programming forums, part of the General Programming Boards category; >system ("cls"); The system function should be avoided wherever possible. First is the portability issue (system is portable, but the ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Arrow system("cls")

    >system ("cls");
    The system function should be avoided wherever possible. First is the portability issue (system is portable, but the argument cannot be), then there is the speed issue (system is very slow). Most of the time people clear the screen when they really don't need to.
    I found this quote in a thread on the forum.

    (1) Is there a faster, better, or more efficient way to do this?

    -scrolling the screen is unacceptable.
    e.g.
    Code:
    for(int x=0;x<81;x++)
        {
             cout<<"\n";
        }
    (2) What clears the screen that is cross platform compatible (portable)?


    A little further down the post I found this.

    (3) How does hiding the system command make a program more portable?
    Code:
     
    #include <cstdlib>
    
    // Easily changed
    void screen_clear()
    {
      system ( "CLS" );
    }
    
    int main()
    {
      clear(); // Calls clear a lot, but that's okay. Porting is easier
    }
    Last edited by xviddivxoggmp3; 12-07-2003 at 07:24 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    161
    (1) There are, but none are portable. Check the FAQ.
    (2) See above.
    (3) The program is more portable because you only have to make a change in one function if you move the program to a system where the current clear screen method doesn't work.

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    There is no real way to portably clear the screen (is portably a word?). The only possible way would be to use assembler, but different platforms would have different methods of accessing the screen.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >(1) Is there a faster, better, or more efficient way to do this?
    Yes, but exactly what that way is will be implementation dependent. As an example, some compilers support a function called clrscr() in conio.h that clears the screen. This is a better option than the system function. Where you don't have the luxury of a prewritten compiler extension, you have to write your own. For that, please see our FAQ.

    >-scrolling the screen is unacceptable.
    Indeed it is. Unless you also have a way of moving the cursor back to a 0,0 location, this solution is ugly. It's also not wise since the actual size of the screen is usually unknown, so you can't be sure that you are clearing all of the screen or too much. You can find the dimensions if a screen is present, but after that this solution loses its novelty. Then there's the added portability problem of the C++ standard not even requiring that a screen be present.

    >(3) How does hiding the system command make a program more portable?
    Rather than sprinkling nonportable constructs all over your program and in the middle of portable constructs, you isolate them so that only one segment of your code has to be changed when the program is ported to another system. The alternative is to change every single occurance of the nonportable construct. As you can imagine, for larger projects, this would be a nightmare. Using the example you gave (that I gave originally), to port this code to, say, Unix, instead of changing every system call (which could number in the thousands for a large program), you would need to change only one thing, the implementation of screen_clear.

    Note: I just noticed that I made a boo-boo. I define the function as screen_clear, but call it as clear. Baka.
    My best code is written with the delete key.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I just use option 3 if i have to, but i try to avoid clearing the screen as much as possible...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    Thank you for the input.

    Follow up question:
    (1) Is there a list of constructs (code) that is non-portable?
    -I feel the best way is like you said confine the non-portable code to an individual function.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    > Is there a list of constructs (code) that is non-portable?
    If you wrote the function yourself, then it's portable
    If the function you call is part of the standard library, then it's portable
    Anything else is to some extent system specific.

    > -I feel the best way is like you said confine the non-portable code to an individual function.
    This is a good plan
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cls() vs system("cls")
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 10-26-2006, 03:13 PM
  2. just a question about the system("cls") command
    By Cmuppet in forum C Programming
    Replies: 8
    Last Post: 08-05-2004, 07:58 PM
  3. gcc and process.h
    By xddxogm3 in forum C Programming
    Replies: 7
    Last Post: 03-18-2004, 05:14 PM

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