Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
–
You can print something like "Press any key to continue..." before that. Some people will tell you about
system("pause");
But don't use it. It's not portable.
–
–
The function waits for a single keypress and returns its (integer) value.
For example, I have a function that does the same as System("pause")
, but without requiring that "pause.exe" (which is a potential security whole, btw):
void pause()
std::cout << std::endl << "Press any key to continue...";
getchar();
–
–
The incorrect solution would be to use system("pause")
as this creates security holes (malicious pause.exe in directory!) and is not cross-platform (pause only exists on Windows/DOS).
There is a simpler solution:
void myPause() {
printf("Press any key to continue . . .");
getchar();
This uses getchar()
, which is POSIX compliant (see this).
You can use this function like this:
int main() {
myPause();
This effectively prevents the console from flashing and then exiting.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.