system("pause"); - Why is it wrong?
Solution 1:
It's frowned upon because it's a platform-specific hack that has nothing to do with actually learning programming, but instead to get around a feature of the IDE/OS - the console window launched from Visual Studio closes when the program has finished execution, and so the new user doesn't get to see the output of his new program.
Bodging in System("pause") runs the Windows command-line "pause" program and waits for that to terminate before it continues execution of the program - the console window stays open so you can read the output.
A better idea would be to put a breakpoint at the end and debug it, but that again has problems.
Solution 2:
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?
Solution 3:
- slow: it has to jump through lots of unnecessary Windows code and a separate program for a simple operation
- not portable: dependent on the pause program
- not good style: making a System call should only be done when really necessary
- more typing: System("pause") is longer than getchar()
a simple getchar() should do just fine.