Interested in System()

Jan 17, 2011 at 6:47pm
Since starting c++ programming 4 months ago in my highschool class, When I learned system commands, I thought "wow these are helpful, (for ex, system("cls"), system("pause"), system(color ##") although color I do agree we can do without) why are these frowned upon? I use them all the time because I know no alternative, except for color :D. Can people list some alternatives for these
system("cls")
system("pause")

and reading through the forums, I saw someone said the alternative for cls is not using it, but why wouldn't I want to clear the screen? Because I dont want all of my output to cludder in larger program(A console game I'm creating for school for example).
Jan 17, 2011 at 6:55pm
The main reason not to use it is that it is not portable. The system() command adds external dependencies. You cannot take that program that does "cls" on Windows and then run it on Mac or Linux.
Jan 17, 2011 at 6:58pm
But then what do professional programmers, well if they even do console based programs, use instead of cls? is there a c++ based screen clearing command?
Jan 17, 2011 at 7:19pm
no, which is why you write one.
Jan 17, 2011 at 7:19pm
They do not clear the screen. Very few console program shipped with windows or Unix do this. Look at "dir" (or "ls" on Unix) for example. If the user wants the screen cleared, they can write a batch or shell script that does it for them.
Jan 17, 2011 at 7:26pm
+1 PanGalactic

Most of these things are things console programs shouldn't do because it interferes with other console programs.

Console programs are often run in batches, so that when one program finishes, another one starts -- allowing you to complete a somewhat complex task quickly and without interruption.

The problem with these things is that they break that smooth chain.

"pause" prompts the user for input. This is bad in a console environment because it breaks the automation of batch program running. If every program paused, you'd have to pointlessly hit enter a dozen times whenever you ran a batch file.

"cls" clears the screen. This is bad in a console environment because it erases the output of all programs that ran previously, not allowing the user to see the result of those programs.
Jan 17, 2011 at 7:53pm
well, and I know the obligatory article will be posted. Should you be writing a console based game a clear function may be in order. Now whether or not this is relevant due to its quality of choice, I'll leave up to anyone else.
Jan 17, 2011 at 8:13pm
My program, outputs alot of information that becomes irrelevant to current input in the program, usually I group pause and cls together." so user inputs, switch chooses output, couts input, pauses, clears the screen, and restarts with new information(making the original input, output, unnecessary to still have displayed"
I do understand now why its frowned upon though, but in a program, should I create a void function once at the top using system("cls"); and just call the function whenever i'd like to clear the screen?(instead of putting cls ever 10 lines or so.) and from now on instead of pause use that cin.get?
Last edited on Jan 17, 2011 at 8:15pm
Jan 17, 2011 at 8:17pm
Don't use system at all, if you need to clear the screen search the API for the operating system you are developing for, for the appropriate system calls.
Jan 17, 2011 at 8:23pm
Alright, I'm just planning on not clearing the screen anymore, i'll use someone else methods of skipping a large amount of lines so the screen appears to be cleared
cout << string(50, '\n'); Thanks "PersonJerry"

for now I'm not using any graphics API so this'll do
Last edited on Jan 17, 2011 at 8:24pm
Jan 17, 2011 at 11:16pm
Topic archived. No new replies allowed.