138

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
Ivars
  • 2,255
  • 5
  • 19
  • 29
  • 2
    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 '14 at 12:12
  • 7
    If you want to pause until pressing enter: `std::cin.ignore();` – Stevoisiak Aug 02 '18 at 17:01

4 Answers4

252

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.

Chnossos
  • 9,164
  • 4
  • 25
  • 38
herohuyongtao
  • 47,739
  • 25
  • 124
  • 164
  • 3
    In Microsoft VS2012, Use include #include and _getch(). – CreativeMind Jan 21 '14 at 12:14
  • @herohuyongtao: `getchar` would expect _Enter_. [See this](http://stackoverflow.com/q/1798511/183120). – legends2k Jan 21 '14 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." – CreativeMind Jan 21 '14 at 12:28
  • @RonakPatel I don't use Visual Studio. Would those same includes apply for G++ as well? – RPiAwesomeness Nov 25 '14 at 02:06
  • 3
    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. – Roger Sanders Jul 01 '15 at 04:36
  • system("pause") worked for me, it seems like the simplest solution... I'm on VS Community Edition 2013. – Greg Oct 17 '15 at 00:10
  • 53
    `system("pause")` does not work on linux/unix, I would specify that in the answer. – Zimano Jan 29 '16 at 11:48
  • @Zimano No, it will work on every standard platform as long as it supports C, check out [here](http://stackoverflow.com/a/4622710/2589776). – herohuyongtao Jan 29 '16 at 12:49
  • 6
    @herohuyongtao No, it doesn't. Please compile an application on Linux/Unix making a call to system("pause") and see for yourself. I am not talking about the system()( function. But the use of "pause" as a command. – Zimano Jan 29 '16 at 14:51
  • `system("pause")` does not work on Ubuntu 15.10. Not sure about other versions of Ubuntu. – Marcin May 12 '16 at 23:57
  • system("pause") does not work on Ubuntu 14 – programmer44 Oct 10 '16 at 02:44
  • 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 – Swift - Friday Pie Feb 02 '17 at 14:31
  • @Swift-FridayPie Please don't use `pause` from unistd.h. It waits for a signal, not a user input: https://stackoverflow.com/a/15992981/5601591. Please, please at least have the courtesy to use `system("pause")` only when `_WIN32` is defined and wait for a line break everywhere else. I hope that's not asking too much. – Jack G Oct 20 '21 at 20:27
  • @JackG necrocomment, but that's fine, I never wrote it wait fr user input, yeah, though that quite established way to wait (or even wrap-up process, e.g. how old versions ffmpeg did it). User have to send signal by Ctrl-C or its analog in this case, which they usually can. How "less"\"more" do work? That's where it should be looked up. Neither only waiting for line break is a good way, on some systems you may end with frozen terminal instance if user has been disconnected from host. Wait for nd-of-transmission but also setup signal handlers, perhaps? – Swift - Friday Pie Oct 21 '21 at 06:03
18

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

Volomike
  • 22,513
  • 20
  • 109
  • 197
CMS
  • 686
  • 3
  • 9
  • 31
7

You can try

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

int main() {

    //some codes

    getch();
    return 0;
}
Edward Falk
  • 9,685
  • 9
  • 73
  • 108
JustCode
  • 105
  • 5
7

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.

CashCow
  • 29,989
  • 4
  • 56
  • 89