Here\'s a question that I don\'t quite understand:
The command, system(\"pause\");
is taught to new programmers as a way to pause a program and wait for
Because it is not portable.
pause
is a windows / dos only program, so this your code won't run on linux. Moreover, system
is not generally regarded as a very good way to call another program - it is usually better to use CreateProcess
or fork
or something similar.
As listed on the other answers, there are many reasons you can find to avoid this. It all boils down to one reason that makes the rest moot. The System()
function is inherently insecure/untrusted, and should not be introduced into a program unless necessary.
For a student assignment, this condition was never met, and for this reason I would fail an assignment without even running the program if a call to this method was present. (This was made clear from the start.)
It's slow. It's platform dependent. It's insecure.
First: What it does. Calling "system" is literally like typing a command into the windows command prompt. There is a ton of setup and teardown for your application to make such a call - and the overhead is simply ridiculous.
What if a program called "pause" was placed into the user's PATH? Just calling system("pause") only guarantees that a program called "pause" is executed (hope that you don't have your executable named "pause"!)
Simply write your own "Pause()" function that uses _getch. OK, sure, _getch is platform dependent as well (note: it's defined in "conio.h") - but it's much nicer than system()
if you are developing on Windows and it has the same effect (though it is your responsibility to provide the text with cout or so).
Basically: why introduce so many potential problems when you can simply add two lines of code and one include and get a much more flexible mechanism?
a simple getchar() should do just fine.
For me it doesn't make sense in general to wait before exiting without reason. A program that has done its work should just end and hand over its resources back to its creator.
One also doesn't silently wait in a dark corner after a work day, waiting for someone tipping ones shoulder.
Here's one reason you shouldn't use it: it's going to piss off most anti-virus programs running on Windows if you're passing the program over to another machine because it's a security threat. Even if your program only consists of a simple cout << "hello world\n"; system("pause");
It's resource heavy and the program gets access to the cmd command, which anti viruses see as a threat.