Which is best way to pause the console in C++ programs?
cin.get()
system("pause")
getch()
or getchar()
?Is it true that use of system("pause")
leads to non portable code and can't work in UNIX?
Is cin.get() is better to use to pause console?
Some common uses of system() in Windows OS are, system(“pause”) which is used to execute pause command and make the screen/terminal wait for a key press, and system(“cls”) which is used to make the screen/terminal clear.
Using system(“pause”) command in C++ This is a Windows-specific command, which tells the OS to run the pause program. This program waits to be terminated, and halts the exceution of the parent C++ program. Only after the pause program is terminated, will the original program continue.
The function system (“pause”) is utilized to stop the program at any time or the termination of the code and obtain the outcome of the code on the console terminal. The function system pause in C++ is typically utilized when the user desires to get the outcome in a console window.
Press CTRL+F5.
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:
#define
is hellInstead, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With