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 If this is Windows, then it's a duplicate of the one @Christian linked to. Voted to close. sbi Dec 3, 2010 at 8:49

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.

Note that while cin.get() is supposed to read a single character it's probable that your terminal does line buffering so that you will have to press enter before the call to get() returns. – Alex Jasmin Dec 3, 2010 at 8:54 Behavior of those two samples doesn't behave the same, cin.get() waits for ENTER, and system("pause") returns immediately once user presses ANY key. – SoLaR Jun 6, 2021 at 14:30

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();
                Well, I was under the impression that the command pause resulted in running a whole process which is basically the program "pause". For example, system("notepad") (on windows) starts notepad, so this command definitely can launch other executables.
– Mephane
                Dec 8, 2010 at 11:52
                The C++ standard library is incorporated in the ANSI/C++ ISO Language standard and contains std::cin of class istream. This is cross-platform and not "nothing".
– Abel
                Apr 8, 2012 at 15:27

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.