A system() question

This is a discussion on A system() question within the C++ Programming forums, part of the General Programming Boards category; Ok I know your not supposed to use it. Ive seen people say not to use it many times. I ...

  1. #1
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60

    A system() question

    Ok I know your not supposed to use it. Ive seen people say not to use it many times. I was just wandering why not use the system(). I havent actualyl seen anyone say why its bad to use.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    security holes, non-portability...

    every time you put system("cls") in your program, my computer doens't clear the screen... what it does is print a newline followed by "bash: cls: command not found" followed by another newline, then your program keeps on going.

    on the other hand, what happens if I somehow get into your computer and remove cls.exe and replace it with my own program called cls.exe, that does a little more than clear the screen.

    now when you run your program on your own machine, to you everything looks just peachy, but you could have just launced the most dangerous virus written on the planet

    you're also spawning off another program to do your dirty work, and you don't have much control over the process once you do so.
    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

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    what major_small is true -- for programs you intend to give to someone else. But I have no whelms about using it for quick-and-dirty programs I write and don't want to bother implementing all that code in my program (it is not a trivel thing to do).

    If someone has replaced cmd.com on my computer then I have much bigger problems than to worry about what system("cls") might do.

  4. #4
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    Oh ok...that sounds pretty bad. Ill have to stay away from that.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Ancient Dragon
    what major_small is true -- for programs you intend to give to someone else. But I have no whelms about using it for quick-and-dirty programs I write and don't want to bother implementing all that code in my program (it is not a trivel thing to do).

    If someone has replaced cmd.com on my computer then I have much bigger problems than to worry about what system("cls") might do.
    yeah, i gotta agree here - although I stress not using it, I use it for my own programs, although not to pause, and rarely to clear the screen... but for programs other people will see or use I dont' use it.

    think of it as cutting corners and throwing caution to the wind.

    one way you can make yourself feel better about it is by using wrapper functions - write something like this into your code:
    Code:
    void clearScreen(){system("clear");}
    now you use clearScreen(); wherever you'd need to clear the screen, and you can always change the code in there whenever you move it to another system.
    Last edited by major_small; 03-30-2006 at 09:45 PM.
    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
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    In case anyone does not know, AdrianXW has a tutorial on one method of clearing the screen without using the system. Me personally I use system("cls") all the time but I never thought about the portability issue. I may have to change how I do a few things.
    Check oiut Adrian's web sit here
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    adrian's site is all nice and good, but it's still not portable

    he's focusing on windows programming, so he's using ostensibly safe methods to do those things in windows. Bottom line: there is no standard portable way to clear the screen - you have to make a compromise somewhere.
    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

  8. #8
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    Like I said, he shows one way to do it.

    When all else fails, you can always do a bunch of cout<<endl's untill everything goes off the screen. Its a bit ugly, but it will work
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by rwmarsh
    Like I said, he shows one way to do it.

    When all else fails, you can always do a bunch of cout<<endl's untill everything goes off the screen. Its a bit ugly, but it will work
    it'd be better to ouput a whole bunch of newlines followed by an endl - flushing the buffer every time is slow.
    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

  10. #10
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    Well I found a way, not to clear the screen, but at least to pause it.

    Code:
    int main()
    {
    cout << " line.\n";
    cout << " line.\n";
    cout << " line.\n";
    cout << " line.\n";
    cout << " line.\n";
    cin.get(); // Shows first 5 lines. When user is ready they just press enter to continue.
    cout << " line.\n";
    cout << " line.\n";
    cout << " line.\n";
    cout << " line.\n";
    cout << " line.\n";
    }
    Works good enough for me. I counted so many lines, then placed cin.get() there. I have it set up to where like 19 lines are shown on screen. With a few "\n" after it so it takes up more lines but nothing is there. Then when enter is pressed, it appears as if the screen is being cleared and more text appears. Works fine for me.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    "portability" is only important when you write programs you know will be targed for more than one operating system. If you are writing MS-Windows programs with win32 api function calls, "portability" has been tossed to the wind anyway so you might as well use system("cls"). Or if you are writing a *nix program with *nix-specific functions then use system("clear"). And the security concern about what would happen if someone changed cmd.com is also a non-issue -- if someone did that then the os has a much larger problem then my using system() function because a lot of other programs will probably break too.

    New students should not even be concerned about such issues -- tough enough to learn the language.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Ancient Dragon
    New students should not even be concerned about such issues -- tough enough to learn the language.
    but it's tougher to learn it wrong and then have to relearn it later

    don't use system. it spawns a process you have no control over.

    If you're writing windows code, use a windows API to do what you need to do. There's ALWAYS a better alternative to system().
    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

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    When you get to writing useful programs which run as
    prog1 > results
    or
    prog1 | prog2

    The whole 'cls' thing becomes an annoyance you can easily live without.
    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. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 02:52 PM
  2. Question about the System() function...
    By YankeePride13 in forum Linux Programming
    Replies: 4
    Last Post: 11-18-2005, 01:45 PM
  3. System programming question.
    By kiss_psycho in forum C++ Programming
    Replies: 9
    Last Post: 02-21-2003, 03:56 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  5. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 03:31 AM

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