146

What would be the best way to wait for user input in console application?

std::cout << "press any key to exit...";
// wait for user to hit enter or another key
2
  • 3
    There is no standard way to respond to any key press (as console input function waits for the user to hit "enter") just as there is no standard way to respond to mouse operations or any other input device.
    – CashCow
    Jan 21, 2014 at 12:12
  • 8
    If you want to pause until pressing enter: std::cin.ignore();
    – Stevoisiak
    Aug 2, 2018 at 17:01

4 Answers 4

273

Several ways to do so, here are some possible one-line approaches:

  1. Use getch() (need #include <conio.h>).

  2. Use getchar() (expected for Enter, need #include <iostream>).

  3. Use cin.get() (expected for Enter, need #include <iostream>).

  4. Use system("pause") (need #include <iostream>, Windows only).

    PS: This method will also print Press any key to continue . . . on the screen. (seems perfect choice for you :))


Edit: As discussed here, There is no completely portable solution for this. Question 19.1 of the comp.lang.c FAQ covers this in some depth, with solutions for Windows, Unix-like systems, and even MS-DOS and VMS.

10
  • 4
    In Microsoft VS2012, Use include #include <conio.h> and _getch(). Jan 21, 2014 at 12:14
  • @herohuyongtao: getchar would expect Enter. See this.
    – legends2k
    Jan 21, 2014 at 12:18
  • 1
    @herohuyongtao: It gives this error "error C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch." Jan 21, 2014 at 12:28
  • 4
    I'd strongly suggest avoiding the use of getch(), because it only works with a physical console, not stdin, and as such won't work with pipe or file redirection. Jul 1, 2015 at 4:36
  • 4
    for linux, use pause() from unistd.h. pause command exist only on microsoft platform and few pdp or dos related ones. Some distros have it as alias declared Feb 2, 2017 at 14:31
19

a do while loop would be a nice way to wait for the user input. Like this:

int main() 
{

 do 
 {
   cout << '\n' << "Press a key to continue...";
 } while (cin.get() != '\n');

 return 0;
}

You can also use the function system('PAUSE') but I think this is a bit slower and platform dependent

4
  • 5
    system('PAUSE') is not available on all systems...
    – Tiago
    Mar 25, 2015 at 15:50
  • Why does this prompt twice for the first time it is used? Dec 2, 2020 at 21:18
  • 1
    That will loop indefinitely if std::cin is closed (as get() will return EOF). Sep 29, 2021 at 11:18
  • @MarcelloB. Maybe you typed one character before pressing enter(\n). That while loop works until cin.get() gets the '\n' in the above code. E.g. if you type 3 characters and press enter, then it will prompt "Press a key to continue..." 3 times. Jul 20, 2022 at 1:56
10

There is no "standard" library function to do this. The standard (perhaps surprisingly) does not actually recognise the concept of a "keyboard", albeit it does have a standard for "console input".

There are various ways to achieve it on different operating systems (see herohuyongtao's solution) but it is not portable across all platforms that support keyboard input.

Remember that C++ (and C) are devised to be languages that can run on embedded systems that do not have keyboards. (Having said that, an embedded system might not have various other devices that the standard library supports).

This matter has been debated for a long time.

10

You can try

#include <iostream>
#include <conio.h>

int main() {

    //some codes

    getch();
    return 0;
}
7
  • Is getch in main or something?
    – doctorlove
    Jan 21, 2014 at 12:05
  • in main function before return 0; ,i.e. where you want to wait user to input
    – JustCode
    Jan 21, 2014 at 12:07
  • 1
    Don't need to include iostream here. Jan 21, 2014 at 12:16
  • 1
    If there's data in the stdin buffer this will not work...
    – Tiago
    Mar 25, 2015 at 15:48
  • 2
    <conio.h> is a Windows-ism. That might be why some people are having trouble compiling it. The Unix solution is quite ugly, unfortunately. See c-faq.com/osdep/cbreak.html. The basic issue is that Unix buffers input until a newline is received and C's stdin has no way around this. You need to make system calls to turn the buffering off. Nov 19, 2015 at 2:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.