There might be a best way (like using the portable
cin.get()), but a good way doesn’t exist. A program that has done its job should quit and give its resources back to the computer.
And yes, any usage of
system() leads to unportable code, as the parameter is passed to the shell that owns your process.
Having pausing-code in your source code sooner or later causes hassles:
- someone forgets to delete the pausing code before checking in
- now all working mates have to wonder why the app does not close anymore
- version history is tainted
- it’s annoying to anyone who runs your code from the console
- it’s very, very, very annoying when trying to start and end your program from within a script; quadly annoying if your program is part of a pipeline in the shell, because if the program does not end, the shell script or pipeline won’t, too
Instead, explore your IDE. It probably has an option not to close the console window after running. If not, it’s a great justification to you as a developer worth her/his money to always have a console window open nearby.
Alternatively, you can make this a program option, but I personally have never seen a program with an option
Moral of the story: This is the user’s problem, and not the program’s problem. Don’t taint your code.