System("Pause"); - Why Is It Wrong

system(pause); - Why is it wrong?

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" command 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.

Pause Console in C++ program

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
  • #define is hell
  • 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 --keep-alive-when-dead.

Moral of the story: This is the user's problem, and not the program's problem. Don't taint your code.

system(PAUSE) or keep_window_open()?

Are keep_window_open() and system("PAUSE") effectively the same thing, or are they different?

They have an equivalent use-case (block the calling process until user input is entered), but they approach it in different ways.

system("PAUSE") spawns a separate process to run the OS's command-line processor and have it execute a PAUSE command, which outputs a message to the console and then blocks the calling process until user input is entered.

keep_window_open() does not use a separate process, it simply uses std::cout and std::cin to display a console message and then wait for user input, all within in the calling process (this answer shows the actual implementation).

If they are basically the same in function, then is there a reason to use one over the other? If so, when and why?

system("PAUSE") is platform-dependent as it relies on an OS processor being present, and that processor implementing a PAUSE command.

keep_window_open() uses standard C++ features.

If they are different, then is keep_window_open() deprecated in favor of system("PAUSE"), or vice versa?

IMHO, you should use keep_window_open(), since it relies on standard behavior independent of the platform your code is run on. But there is not really anything wrong with using system("PAUSE") instead, if your code runs on a platform that supports it.

If keep_window_open() is still in use, is it a part of the std namespace

keep_window_open() is not part of C++, it is just a plain user-defined function introduced in the book. So no, it is not part of the std namespace.

How do you do a ‘Pause’ with PowerShell 2.0?

cmd /c pause | out-null

(It is not the PowerShell way, but it's so much more elegant.)

Save trees. Use one-liners.

system (pause) in C# won't work. Alternatives?

Go ahead and simply use:

Console.ReadKey();

How can I wait In Node.js (JavaScript)? l need to pause for a period of time

Update Jan 2021: You can even do it in the Node REPL interactive using --experimental-repl-await flag

$ node --experimental-repl-await
> const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
> await delay(1000) /// waiting 1 second.

A new answer to an old question. Today ( Jan 2017 June 2019) it is much easier. You can use the new async/await syntax.
For example:

async function init() {
console.log(1);
await sleep(1000);
console.log(2);
}

function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}

For using async/await out of the box without installing and plugins, you have to use node-v7 or node-v8, using the --harmony flag.

Update June 2019: By using the latest versions of NodeJS you can use it out of the box. No need to provide command line arguments. Even Google Chrome support it today.

Update May 2020:
Soon you will be able to use the await syntax outside of an async function. In the top level like in this example

await sleep(1000)
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}

The proposal is in stage 3.
You can use it today by using webpack 5 (alpha),

More info:

  • Harmony Flag in Nodejs: https://nodejs.org/en/docs/es6/
  • All NodeJS Version for download: https://nodejs.org/en/download/releases/

system(pause) clarification

Do you mean that you want to press any key to continue but not to display the "Press any key to continue" on the screen? Try this getchar(); this will capture one character typing from keyboard and continue.



Related Topics



Leave a reply



Submit